WHERE
clause is used to filter records.It is used to extract only those records that fulfill a specified condition.
WHERE condition;
:
WHERE Country='Mexico';
WHERE CustomerID=1;
SELECT TOP
clause is used to specify the number of records to return.SELECT TOP
clause is useful on large tables with thousands of records. Returning a large number of records can impact performance.Note: Not all database systems support the SELECT TOP
clause. MySQL supports the LIMIT
clause to select a limited number of records, while Oracle uses FETCH FIRST n ROWS ONLY
and ROWNUM
.
SELECT TOP 3 * FROM Customers;
SELECT TOP 50 PERCENT * FROM Customers;
SELECT * FROM Customers
LIMIT 3;
SELECT * FROM Customers
FETCH FIRST 3 ROWS ONLY;
SELECT * FROM Customers
FETCH FIRST 50 PERCENT ROWS ONLY;
ORDER BY
keyword is used to sort the result-set in ascending or descending order.ORDER BY
keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC
keyword.FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
ORDER BY Price;
ORDER BY Country, CustomerName;
The GROUP BY
statement groups rows that have the same values into summary rows, like "find the number of customers in each country".
The GROUP BY
statement is often used with aggregate functions (COUNT()
, MAX()
, MIN()
, SUM()
, AVG()
) to group the result-set by one or more columns.
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;
HAVING
clause was added to SQL because the WHERE
keyword cannot be used with aggregate functions.FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;