Code icon

The App is Under a Quick Maintenance

We apologize for the inconvenience. Please come back later

Menu iconMenu iconPython & SQL Bible
Python & SQL Bible

Chapter 23 - Appendix D: SQL Cheat Sheet

SQL Syntax

  1. Select all columns from a table
SELECT * FROM table_name;
  1. Select specific columns from a table
SELECT column1, column2 FROM table_name;
  1. Select distinct values from a column
SELECT DISTINCT column_name FROM table_name;
  1. Count distinct values from a column
SELECT COUNT(DISTINCT column_name) FROM table_name;
  1. Filter using WHERE
SELECT * FROM table_name WHERE column_name = 'value';
  1. Order by columns
SELECT * FROM table_name ORDER BY column_name ASC|DESC;
  1. 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;
  1. Group by columns
SELECT COUNT(column_name), group_column FROM table_name GROUP BY group_column;
  1. 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

  1. Select all columns from a table
SELECT * FROM table_name;
  1. Select specific columns from a table
SELECT column1, column2 FROM table_name;
  1. Select distinct values from a column
SELECT DISTINCT column_name FROM table_name;
  1. Count distinct values from a column
SELECT COUNT(DISTINCT column_name) FROM table_name;
  1. Filter using WHERE
SELECT * FROM table_name WHERE column_name = 'value';
  1. Order by columns
SELECT * FROM table_name ORDER BY column_name ASC|DESC;
  1. 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;
  1. Group by columns
SELECT COUNT(column_name), group_column FROM table_name GROUP BY group_column;
  1. 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

  1. Select all columns from a table
SELECT * FROM table_name;
  1. Select specific columns from a table
SELECT column1, column2 FROM table_name;
  1. Select distinct values from a column
SELECT DISTINCT column_name FROM table_name;
  1. Count distinct values from a column
SELECT COUNT(DISTINCT column_name) FROM table_name;
  1. Filter using WHERE
SELECT * FROM table_name WHERE column_name = 'value';
  1. Order by columns
SELECT * FROM table_name ORDER BY column_name ASC|DESC;
  1. 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;
  1. Group by columns
SELECT COUNT(column_name), group_column FROM table_name GROUP BY group_column;
  1. 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

  1. Select all columns from a table
SELECT * FROM table_name;
  1. Select specific columns from a table
SELECT column1, column2 FROM table_name;
  1. Select distinct values from a column
SELECT DISTINCT column_name FROM table_name;
  1. Count distinct values from a column
SELECT COUNT(DISTINCT column_name) FROM table_name;
  1. Filter using WHERE
SELECT * FROM table_name WHERE column_name = 'value';
  1. Order by columns
SELECT * FROM table_name ORDER BY column_name ASC|DESC;
  1. 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;
  1. Group by columns
SELECT COUNT(column_name), group_column FROM table_name GROUP BY group_column;
  1. Having clause (used with GROUP BY)
SELECT COUNT(column_name), group_column FROM table_name GROUP BY group_column HAVING COUNT(column_name) > 10;