MySQL Select Statement
In this lesson you will be learn how to use SELECT statement in MySQL and you can also learn how to use SELECT statement with WHERE clause. The SELECT statement is used to retrieve the records from the table. There are some keywords uses in SELECT statement that are described in the following tableKeywords | Description |
SELECT | SELECT statement is used to retrieve fields from one or more tables. |
FROM | Tables containing to the fields. |
WHERE | The WHERE clause is used to describe the criteria to restrict the records retrieved. |
GROUP BY | The GROUP BY clause is used to determine how the records should be grouped. |
HAVING | HAVING clause used with GROUP BY to define the criteria for grouped records |
ORDER BY | The ORDER BY clause is used to described the criteria for ordering the record. |
LIMIT | The LIMIT clause is used to restrict the limit of number of records retrieved. |
mysql> SELECT * FROM Emp; |
mysql> SELECT Name, City, Age from EMP; |
Operator | Description |
= | Equal to |
<> or != | Not equal to |
< | Less then |
< = | Less then or equal to |
> | Greater then |
> = | Greater then or equal to |
Like | Used for comparing string |
Between | Used for checking value between a range. |
IN | Used to check values in a list |
NOT IN | Used to check the value is not in the list. |
mysql> SELECT * FROM Emp WHERE Designation LIKE '%Manager%'; |
mysql> SELECT * FROM Emp WHERE Name LIKE 'R____'; |
mysql> SELECT * FROM Emp WHERE Salary BETWEEN 10000 AND 20000; |
mysql> SELECT * FROM Emp WHERE Designation ='Manager' OR 'Assistant'; |
mysql> SELECT * FROM Emp WHERE Designation IN ('Manager', 'Assistant'); |
mysql> SELECT * FROM Emp WHERE Designation NOT IN ('Manager', 'Assistant'); |
- AVG( );
The AVG( ) function returns the average value in a group of records. Example of the AVG( ) function:-SELECT AVG(Profit) FROM Income GROUP BY EmpId; - COUNT( );
The COUNT( ) function returns the number of records in a group of records. Example of the COUNT( ) function:-SELECT COUNT(Profit) FROM Income GROUP BY EmpId; - MAX( );
The MAX( ) function return the maximum value in a group of records. Example of the MAX( ) function:-SELECT MAX(Profit) FROM Income GROUP BY EmpId; - MIN( );
The MIN( ) function returns minimum value in a group of records. Example of the MIN( ) function:-SELECT MIN(Profit) FROM Income GROUP BY EmpId; - SUM( );
The SUM( ) function return the sum of the field. Example of the SUM() function :SELECT SUM(Profit) FROM Income GROUP BY EmpId ;
As you know the WHERE clause is used to restrict the records in a query. But if you want to restrict the records by using the Aggregate function then you have to use the HAVING clause. The HAVING clause restricts the records after they have been grouped. The following example shows the list of all Employees who did profit over 10000 on average.
mysql> SELECT AVG(Profit) FROM Income GROUP BY EmpId HAVING AVG(Profit) > 10000; |
The ORDER BY clause can be used to set the order of the retrieved records. The following example shows the list of all employees in the Emp table in alphabetical order. In this clause we can use the ASC or DESC modifiers to set the order of records in ascending or descending order. If any modifier is not provided then the records are listed in ascending order.
mysql> SELECT Name FROM Emp ORDER BY Name; |
The LIMIT clause can be used to limit the number of records that have been returned by the SELECT statement. You can specify the start row, and number of records retrieved.
mysql> SELECT * FROM Emp LIMIT 0,10; |
No comments:
Post a Comment