0 day streak
Code library

Beginner SQL exercises

Read these Beginner SQL snippets line by line — each one comes with a written breakdown of what the code does and why.

SQL · Beginner

Basic Data Retrieval

The SELECT statement is used to fetch data from a database. This query retrieves two specific columns: first_name and last_name from the employees table.

SyntaxQueries & Joins
Open snippet →
SQL · Beginner

Creating a Table

The CREATE TABLE statement is used to create a new table in a database. You must define the name of the columns and their data types.

SyntaxData Types
Open snippet →
SQL · Beginner

Sorting Results

The ORDER BY keyword is used to sort the result-set in ascending (ASC) or descending (DESC) order. Here, products are sorted from most expensive to cheapest.

Queries & JoinsSyntax
Open snippet →
SQL · Beginner

Filtering with WHERE

The WHERE clause is used to filter records. It ensures that only rows meeting a specific condition—in this case, products with a price greater than 100—are returned.

SyntaxQueries & Joins
Open snippet →
SQL · Beginner

Aggregate Functions: COUNT

The COUNT() function returns the number of rows that matches a specified criterion. Using (*) counts all rows in the orders table.

FunctionsQueries & Joins
Open snippet →
SQL · Beginner

Updating Existing Data

The UPDATE statement is used to modify the existing records in a table. It is crucial to use a WHERE clause to specify which record(s) should be updated; otherwise, all records in the table will be…

SyntaxQueries & Joins
Open snippet →
SQL · Beginner

Joining Tables with INNER JOIN

INNER JOIN combines rows from two tables whenever there is a matching value in a common column. This allows you to retrieve data distributed across multiple tables in a single result set.

SyntaxQueries & Joins
Open snippet →
SQL · Beginner

Using Column Aliases

Aliases are used to give a table or a column in a table a temporary name. They are often used to make column names more readable or descriptive in the output.

SyntaxBest Practices
Open snippet →
SQL · Beginner

Retrieving Unique Values

The DISTINCT keyword is used in a SELECT statement to remove duplicate rows from the result set. It ensures that each returned value is unique.

SyntaxQueries & Joins
Open snippet →
SQL · Beginner

Deleting Data

The DELETE statement removes rows from a table. Similar to UPDATE, the WHERE clause is vital to prevent accidentally deleting all data in the table.

SyntaxQueries & Joins
Open snippet →
SQL · Beginner

Enhancing Database Security with Views

A view provides a restricted virtual table that exposes only non-sensitive columns, preventing unauthorized access to confidential fields like passwords or social security numbers.

SecurityBest Practices
Open snippet →