Simple SQL Queries for Beginners: SELECT, ORDER BY, CASE WHEN

Jun 17, 2024

In this video, we'll walk through some basic SQL queries using a small, generated table. This setup allows you to follow along without needing any pre-existing database.

Getting Started

We'll use an SQL query cheat sheet, available for free download in the video description. The queries are executed live on an SQL server, but they should work on other databases as well. The data is generated on the fly, making it easy for you to replicate.

Our Table Structure

EmployeeId DepartmentId First Name Last Name Salary ManagerId
1 1 Ben Smith 100000 NULL
2 1 John Miller 80000 1
3 2 Ed Jones 65000 1
4 2 Ben Holmes 70000 1
5 NULL Norman Brown 1000 1

Basic SQL Queries

Select All Data

To select all data from the table:

Select Specific Columns

To retrieve specific columns, such as first_name and last_name:

Ordering Data

To sort the data by a specific column:

To sort in descending order:

Limiting Results

To limit the number of rows returned:

Distinct Values

To get unique values from a column:

Conditional Statements

To add conditional logic with the CASE WHEN statement:

Conclusion

These basic SQL queries will help you get started. For more detailed instructions and examples, download the SQL Query Cheat Sheet and follow along with the queries in the GitHub repository. Happy querying!

Are you struggling to remember the SQL syntax?

Get FREE SQL Query Cheat Sheet