Académique Documents
Professionnel Documents
Culture Documents
Select components
1. Select (required)
2. Expression (required)
3. From Table (Only optional if table data is not
used)
4. Where Condition (optional)
5. Group By (optional)
6. Having (optional)
7. Order By (optional)
8. Limit (optional)
Select
Is a MySQL keyword at the beginning of a
Structured Query Language (SQL) statement or
more simply a query that retrieves data from a
database.
It can be used in conjunction with the MySQL
command insert, replace, and create table.
Expression
The expression
1.
2.
Examples
Select * from table
Alias
SQL aliases are used to give a database table, or a column in a
table, a temporary name.
Syntax
SELECT column_name AS alias_name ---- column alias
FROM table_name;
SELECT column_name(s)
FROM table_name AS alias_name; ---- table alias
Example
Distinct
The SELECT DISTINCT statement is used to return
only distinct (different) values.
SELECT DISTINCT column_name,column_name
FROM table_name;
Example
SELECT DISTINCT City FROM Customers;
From
Tells MySQL which table(s) to select from
If multiple tables are used in the expression, they all must be
listed separated by a ,
Example
SELECT CustomerName AS Customer, ContactName FROM Customers;
SELECT Orders.OrderID, Orders.OrderDate, Customers.CustomerName
FROM Customers, Orders WHERE Customers.CustomerName="Around the
Horn" AND Customers.CustomerID=Orders.CustomerID;
Where
The WHERE clause is used to filter records. It sets the condition of which rows
to select. Useful for eliminating unwanted data from your results.
SQL WHERE Syntax
SELECTcolumn_name,column_name
FROMtable_name
WHEREcolumn_name operator value;
Conditional
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
= Equals
!= Not equals
Logical
and True if both are true
or True if only one is true
() Useful for grouping or
ordering multiple logical
statements
Where Examples
SELECT * FROM Customers WHERE Country='Mexico';
SELECT * FROM Customers WHERE CustomerID=1;
SELECT * FROM Customers WHERE Country='Germany AND
City='Berlin';
SELECT supplier_id FROM suppliers WHERE supplier_name =
'Apple' OR supplier_name = 'Microsoft';
SELECT * FROM suppliers WHERE (state = 'Florida' AND
supplier_name = 'IBM') OR (supplier_id > 5000);
SELECT * FROM Customers WHERE City LIKE 's%';
SELECT * FROM Customers WHERE City LIKE '_erlin';
SELECT * FROM Customers WHERE City LIKE 'L_n_on';
Like
The LIKE operator is used in a WHERE clause to search for a
specified pattern in a column.
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;
Example
SELECT
SELECT
SELECT
SELECT
*
*
*
*
FROM
FROM
FROM
FROM
Customers
Customers
Customers
Customers
WHERE
WHERE
WHERE
WHERE
Between
The BETWEEN operator is used to select values within a range.
SQL BETWEEN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Example
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
SELECT * FROM Products
WHERE Price NOT BETWEEN 10 AND 20;
SELECT * FROM Products
WHERE ProductName BETWEEN 'C' AND 'M';
In Operator
The IN operator allows you to specify multiple
values in a WHERE clause.
SQL IN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...);
Example
SELECT * FROM Customers
WHERE City IN ('Paris','London');
SELECT * FROM Customers
WHERE City NOT IN ('Paris','London');
Order By
The ORDER BY keyword is used to sort the result-set by one or more
columns.
The ORDER BY keyword sorts the records in ascending order by
default. To sort the records in a descending order, you can use the
DESC keyword.
SQL ORDER BY Syntax
SELECT column_name,column_name
FROM table_name
ORDER BY column_name,column_name ASC|DESC;
Example
SELECT
SELECT
SELECT
SELECT
*
*
*
*
FROM
FROM
FROM
FROM
Customers
Customers
Customers
Customers
ORDER
ORDER
ORDER
WHERE
BY
BY
BY
ID
Country;
Country DESC;
Country,CustomerName;
BETWEEN 1 and 100 ORDER BY CustomerName;
USING FUNCTIONS
SQL aggregate functions return a single value,
calculated from values in a column.
Useful aggregate functions:
Using Functions
SQL Scalar functions
SQL scalar functions return a single value, based on the
input value.
Useful scalar functions:
Example
SELECT AVG(Price) AS PriceAverage FROM Products;
SELECT ProductName, Price FROM Products
WHERE Price>(SELECT AVG(Price) FROM Products);
COUNT(*) Syntax
The COUNT(*) function returns the number of records in a table:
SELECT COUNT(*) FROM table_name;
Group By
Aggregates rows by distinct values in the columns listed in the group by
when using statistical functions (e.g., avg, std, max, min, sum, count, etc.)
Syntax
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;
Example
SELECT Rep_Num, AVG(Balance)
FROM Customer
GROUP BY Rep_Num
ORDER BY Rep_Num
SELECT Rep_Num, AVG(Balance)
FROM Customer
GROUP BY Rep_Num, Customer_Name
ORDER BY Rep_Num
Having
The HAVING clause was added to SQL because the WHERE keyword could not be used
with aggregate functions.
WHERE vs HAVING
Where used to limit the rows that are included in a querys result
Having used to limit the groups that are included
Example
SELECT Rep_Num, AVG(Balance)
FROM Customer
GROUP BY Rep_Num
HAVING COUNT(*)<4
ORDER BY Rep_Num;
LIMIT
Limits the number of rows from the result set
Example
SELECT *
FROM CUSTOMERS
WHERE Balance <5000
LIMIT 10; ----- 10 rows will be displayed as output
NULLS
NULL values represent missing unknown data. By
default, a table column can hold NULL values.
Example
SELECT LastName,FirstName,Address FROM Persons
WHERE Address IS NULL
SELECT LastName,FirstName,Address FROM Persons
WHERE Address IS NOT NULL