SQL Interview Cheat Sheet

Updated on

December 13, 2024

Download our SQL Interview Cheat Sheet, and use it as a reference for your next SQL interview. Click below to download the SQL cheat sheet PDF and start preparing like a pro!

[Download the SQL Interview Cheat Sheet]

SQL Interview Cheat Sheet

In case you want to preview the SQL interview cheat sheet, here you go:

SQL Interview Cheat Sheet

What's Inside the SQL Cheat Sheet?

  • SQL Basics: Quick refreshers on SELECT, WHERE, and JOINs.
  • Advanced Topics: Master Window Functions, CTEs, and Subqueries.
  • Optimization Tips: Learn how to write efficient queries.
  • Practice Questions: Common SQL interview scenarios.

If you need a more in-depth refresher on the SQL commands covered in this SQL interview cheat sheet, try our free SQL Tutorial - it covers everything from basic SQL commands, all the way up to more advanced SQL window functions and joins.

SQL Tutorial

SQL Concepts Covered in the Cheat Sheet

The cheat sheet covered everything from simple concepts like how to query data in a table to more difficult topics like aggregate and window functions.

Querying Data From a Table

Fetch all columns in the Customer Table:


Fetch OrderID and OrderDate Columns from the Orders Table:


Find the discount percentage for all orders:


Round the discount percentage to 2 decimal places:


Sort Output Using ORDERBY

Sort customers by age in the default ASCending order:


Sort customers by age in DESCending order (high to low):


Aliases

AS is used to rename columns:


AS is also used to rename tables:


Filtering Output with WHERE

Fetch customers who are over the age of 35:


Fetch customers who live in a city that starts with “New”:


Fetch customers who live in North America:


Fetch customers that are not minors and not in the USA:


Combining Tables with JOINS

Inner Join


Left Join


Full Join


Right Join


Cross Join


Self Join


SQL Cheat Sheet Joins

Aggregation and Grouping

GROUP BY groups together rows with the same value in specified columns, It computes summaries (aggregates) for each unique combination of values.


Window Functions

The RANK() function is used to assign ranks to rows based on values in the specified column.


The LAG() function is used to access data from a previous row in the same result set without needing a self-join.


SQL Cheat Sheet Window Functions

Partition By

Divides rows into multiple groups, called partitions, to which the window function is applied.


Order By

Specifies the order of rows in each partition to which the window function is applied.


Subqueries

This query finds orders with the same order cost as order_id 101.


This query finds orders in cities that have a cost of over 200.


CTE’s

This query calculates the total sales per customer and then filters customers with total sales above 500.


SQL Cheat Sheet CTE

Looking for More SQL Interview Resources?

A SQL Interview cheat sheet is a good way to refresh your understanding before an interview, but the best way to truly get ready is to practice tons of real SQL interview questions. Tackle these FAANG SQL interview questions to get started:

You can also practice SQL interview questions by concept or topic:

And if you’re looking for an all-around resource for conquering the Data Science Interview read this Amazon #1 Best selling book: Ace the Data Science Interview.

Ace the Data Science Interview

© 2024 DataLemur, Inc

Career Resources

Free 9-Day Data Interview Crash CourseFree SQL Tutorial for Data AnalyticsSQL Interview Cheat Sheet PDFUltimate SQL Interview GuideAce the Data Job Hunt Video CourseAce the Data Science InterviewBest Books for Data Analysts