SQL Query Commands
Fundamentals
- select all rows and columns from table tbl
SELECT * FROM tbl;
- select column c1, c2 and all rows from table tbl
SELECT c1, c2 FROM tbl;
- DISTINCT: search for unique values
SELECT DISTINCT c1, c2 FROM tbl;
- WHERE: query for specific rows
1 2
SELECT c1 FROM tbl WHERE conditions;
- COUNT: the number of rows taht match a specific condition of a query
1
SELECT COUNT(*) FROM tbl;
1
SELECT COUNT(DISTINCT *) FROM tbl;
- LIMIT: limit the number of rows you get back after query. We can use this command when we care more about columns than rows
1 2
SELECT * FROM tbl LIMIT 5;
- ORDER BY: sort the rows in ascending or descending order
1 2
SELECT c1, c2 FROM tbl ORDER BY c2 ASC;
Note: ASC denotes ascending order(default), DESC denotes descending order
- BETWEEN: match a value against a range of values. Can use <, > mathematical functions instead. e.g.
1 2
SELECT amount, payment_date FROM payment WHERE payment_date BETWEEN '2014-04-06' AND '2014-05-06'
- IN: use with WHERE clause to check if a value in a list of values. e.g.
1 2
SELECT c1 FROM tbl WHERE c1 IN (value1, value2, ...);
Use sub query:
1 2
SELECT c1 FROM tbl1 WHERE c1 IN (SELECT c2 FROM tbl2);
- LIKE: match pattern. e.g.
1 2
SELECT first_name FROM customer WHERE first_name LIKE 'Jen%';
- wildcard
- %: match any sequence of chars
- -: match any single char Note: LIKE is case-sensitive, whereas ILIKE is case-insensitive
- wildcard
GROUP BY Statements
- aggregate functions: MAX, MIN, AVG, SUM, ROUND, COUNT…
1 2
SELECT ROUND(AVG(amount), 5) FROM payment; SELECT MIN(amount) FROM payment;
- GROUP BY: divides the rows returned from the SELECT statement into groups. For each group, you can apply an aggregate function.
1 2 3
SELECT c1, aggregate(expr) FROM tbl GROUP BY c1
GROUP BY will group by distinct values. For each distinct value, we do aggregate function.
- HAVING: use with GROUP BY clause to filter group rows that do not satisfy a specific condition
1 2 3 4
SELECT c1, aggregate(expr) AS c2 FROM tbl GROUP BY c1 HAVING c2 > v;
Note: WHERE and HAVING are similar. But WHERE applies before GROUP BY and HAVING applies after GROUP BY
- AS: rename columns or table selections with an alias
JOINS
- INNER JOIN
1 2 3
SELECT A.pka, A.c1, B.pkb, B.c2 FROM A INNER JOIN B ON A.pka = B.fka;
returns rows in A table that have the corresponding rows in B table
There are several types of JOIN: INNER JOIN, FULL OUTER JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN.
- UNION: combines result sets of two or more SELECT statements into a single result set
1 2 3 4 5
SELECT c1, c2 FROM t1 UNION SELECT c1, c2 FROM t2;
We usually use UNION to combine data from similar tables that are not perfectly normalized
Advanced Commands
- timestamp Each type of database has timestamp datatype. You can check documentation for details Examples:
1 2 3 4 5
SELECT SUM(amount), extract(month from payment_date) AS month FROM payment GROUP BY month ORDER BY SUM(amount) DESC LIMIT 1;
- mathmatical functions SQL comes with a lot of mathematical operators built-in that are very useful for numeric column types. You can check documentation for details. Examples:
1 2
SELECT customer_id + rental_id AS new_id FROM payment;
- string function and operations SQL comes with a lot of string operations. Examples:
1 2
SELECT first_name || ' ' || last_name AS full_name FROM customer;
where || denotes string concatenation.
- SubQuery To construct a subquery, we put the second query in brackets and use it in the WHERE clause as an expression
1 2 3
SELECT film_id, title, rental_rate FROM film WHERE rental_rate > (SELECT AVG(rental_rate) FROM film);
- Self-join: combines rows with other rows in same table. Implement by using table alias
1 2 3 4
SELECT a.customer_id, a.first_name, a.last_name, b.customer_id, b.first_name, b.last_name FROM customer AS a JOIN customer AS b ON a.first_name = b.last_name;
the same as
1 2 3
SELECT a.customer_id, a.first_name, a.last_name, b.customer_id, b.first_name, b.last_name FROM customer AS a, customer AS b WHERE a.first_name = b.last_name;
Comments powered by Disqus.