SQL Flashcards
Benefits of IN
IN executes faster than OR
Can contain another SELECT
NOT
WHERE NOT city = ‘London’ AND NOT city = ‘Dallas’
OR
WHERE name = ‘ali’ OR ‘mamad’
IN
WHERE id IN (9,10,11)
Wildcards
%
‘%Pizza’
‘Pizza%’
‘%Pizza%’
‘S%E’
WHERE size LIKE ‘%pizza’
ORDER BY position
Must always be the last clause in a select statement
Aggregate Functions
AVG() COUNT() MIN() MAX() SUM()
Count all the rows in a table containing values and NULL values
SELECT COUNT (*) AS total_cust FROM customers;
Count all the rows in a specific column ignoring NULL vales
SELECT COUNT (customerID) AS total_cust FROM customers;
DISTINCT on aggregate functions
SELECT COUNT(DISTINCT customerID) FROM customers
Every column in your SELECT statement must be present in a GROUP BY clause, except for
aggregated calculations
WHERE filters on rows, instead use … to filter for groups
HAVING
WHERE vs HAVING
WHERE filters before data is grouped
HAVING filters after data is grouped
Rows eliminated by the WHERE clause will not be included in the group
HAVING
SELECT customerID, COUNT() AS orders
FROM orders
GROUP BY customerID
HAVING COUNT ()>=2;
WHERE clause operators
= <> != >= > <= < BETWEEN AND IS NULL