The Complete Basic querying
SQL Cheat Sheet Using SELECT, AS, and LIMIT in queries.
Created by David Freitag
dataengineeringprojects.io
Using WHERE with logical operators:
Data sample
Here’s the data we’ll be using for our query examples.
Using WHERE for filtering
Using WHERE with comparison operators:
Our first query: SELECT all records from the table.
© 2024 David Freitag dataengineeringprojects.io
Combining operators Wildcards and ORDER
W BY
Using WHERE with AND, OR, NOT, and IN. Using the % wildcard and ordering results.
Grouping and aggregating
W data
Use the GROUP BY keyword along with aggregate functions.
To filter data you’ve aggregated, use HAVING:
© 2024 David Freitag dataengineeringprojects.io
CASE WHEN statements SQL joins W
Use CASE WHEN to create if/then logic in your queries. The 4 main SQL join types:
INNER JOIN
LEFT JOIN
RIGHT JOIN Stack results with
W UNION ALL
DISTINCT for unique values
Use UNION ALL to stack query results together.
Use DISTINCT with SELECT for finding uniques:
FULL OUTER JOIN
© 2024 David Freitag dataengineeringprojects.io
CTE’s and subqueries
Split your query into multiple queries:
Manipulating data
Manipulate data using a variety of functions.
Window functions
W
Use window functions to combine aggregating and
grouping of data.
© 2024 David Freitag dataengineeringprojects.io