You've learned this already. ✅
Click here to view the next lesson.
Chapter 23 - Appendix D: SQL Cheat Sheet
SQL Syntax
- Select all columns from a table
SELECT * FROM table_name;
- Select specific columns from a table
SELECT column1, column2 FROM table_name;
- Select distinct values from a column
SELECT DISTINCT column_name FROM table_name;
- Count distinct values from a column
SELECT COUNT(DISTINCT column_name) FROM table_name;
- Filter using WHERE
SELECT * FROM table_name WHERE column_name = 'value';
- Order by columns
SELECT * FROM table_name ORDER BY column_name ASC|DESC;
- Aggregate Functions
SELECT COUNT(column_name) FROM table_name;
SELECT AVG(column_name) FROM table_name;
SELECT SUM(column_name) FROM table_name;
SELECT MIN(column_name) FROM table_name;
SELECT MAX(column_name) FROM table_name;
- Group by columns
SELECT COUNT(column_name), group_column FROM table_name GROUP BY group_column;
- Having clause (used with GROUP BY)
SELECT COUNT(column_name), group_column FROM table_name GROUP BY group_column HAVING COUNT(column_name) > 10;
SQL Syntax
- Select all columns from a table
SELECT * FROM table_name;
- Select specific columns from a table
SELECT column1, column2 FROM table_name;
- Select distinct values from a column
SELECT DISTINCT column_name FROM table_name;
- Count distinct values from a column
SELECT COUNT(DISTINCT column_name) FROM table_name;
- Filter using WHERE
SELECT * FROM table_name WHERE column_name = 'value';
- Order by columns
SELECT * FROM table_name ORDER BY column_name ASC|DESC;
- Aggregate Functions
SELECT COUNT(column_name) FROM table_name;
SELECT AVG(column_name) FROM table_name;
SELECT SUM(column_name) FROM table_name;
SELECT MIN(column_name) FROM table_name;
SELECT MAX(column_name) FROM table_name;
- Group by columns
SELECT COUNT(column_name), group_column FROM table_name GROUP BY group_column;
- Having clause (used with GROUP BY)
SELECT COUNT(column_name), group_column FROM table_name GROUP BY group_column HAVING COUNT(column_name) > 10;
SQL Syntax
- Select all columns from a table
SELECT * FROM table_name;
- Select specific columns from a table
SELECT column1, column2 FROM table_name;
- Select distinct values from a column
SELECT DISTINCT column_name FROM table_name;
- Count distinct values from a column
SELECT COUNT(DISTINCT column_name) FROM table_name;
- Filter using WHERE
SELECT * FROM table_name WHERE column_name = 'value';
- Order by columns
SELECT * FROM table_name ORDER BY column_name ASC|DESC;
- Aggregate Functions
SELECT COUNT(column_name) FROM table_name;
SELECT AVG(column_name) FROM table_name;
SELECT SUM(column_name) FROM table_name;
SELECT MIN(column_name) FROM table_name;
SELECT MAX(column_name) FROM table_name;
- Group by columns
SELECT COUNT(column_name), group_column FROM table_name GROUP BY group_column;
- Having clause (used with GROUP BY)
SELECT COUNT(column_name), group_column FROM table_name GROUP BY group_column HAVING COUNT(column_name) > 10;
SQL Syntax
- Select all columns from a table
SELECT * FROM table_name;
- Select specific columns from a table
SELECT column1, column2 FROM table_name;
- Select distinct values from a column
SELECT DISTINCT column_name FROM table_name;
- Count distinct values from a column
SELECT COUNT(DISTINCT column_name) FROM table_name;
- Filter using WHERE
SELECT * FROM table_name WHERE column_name = 'value';
- Order by columns
SELECT * FROM table_name ORDER BY column_name ASC|DESC;
- Aggregate Functions
SELECT COUNT(column_name) FROM table_name;
SELECT AVG(column_name) FROM table_name;
SELECT SUM(column_name) FROM table_name;
SELECT MIN(column_name) FROM table_name;
SELECT MAX(column_name) FROM table_name;
- Group by columns
SELECT COUNT(column_name), group_column FROM table_name GROUP BY group_column;
- Having clause (used with GROUP BY)
SELECT COUNT(column_name), group_column FROM table_name GROUP BY group_column HAVING COUNT(column_name) > 10;