Bookmark this page
SQL PlayGround
Quick referece for SQL with practice options
Dataset
We are working with a dataset for a small, fictional animal shelter. We have tables for animals, employees, adoptions, and donations.
Basic Queries
SELECT statement
Select all rows and columns from the Animals table.
Select only some columns (names and species) from the Animals table.
SQL aliases are used to give a table, or a column in a table, a temporary name . An alias is created with the AS keyword in Animals table.
Filtering using WHERE clause
Select only the rows that meet filter conditions.
Use LIKE to get records with partial string match.
Use IN to get records matching a column against a number of values.
Use BETWEEN to get records matching a column against a range of values.
Use IS NULL to get records missing value in a column.
Use NOT operator to do negative check on rows.
Sorting using ORDER BY
You can sort your query results to ORDER BY. By default sort order is Ascending (ASC). You can sort in the Descending (DESC) order also.
LIMIT and OFFSET to limit or skip records.
Limit the results returned from the Animals table to be 4.
Skipping the first two records from the above limited records.
JOINS
INNER Join
Join Animals and Adoptions to list adopted animals and their adopters. (Simple JOIN refers to INNER JOIN itself)
LEFT Join
Get a list of all animals, including those that have not been adopted yet.
RIGHT Join
Get a list of all adoptions, and include animals even if they were not found in the Animals table.
FULL Join or FULL OUTER Join
Join Animals and Adoptions to get a list of all adoptions and all Animals. (UNION of LEFT and RIGHT JOIN can imitate FULL JOIN when it is not supported by certain SQL DBMS like MySQL, SQLite etc...)
Aggregates
Count the number of available animals.
Calculate the average salary of employees.
Sum the total donations received.
Group By
Count the number of animals by species.
Having
Filtering using HAVING clause
Display the position where the sum of salaries is 80,000 or more. The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate functions.
Sub Queries
Find the employee with the highest salary.
List all animals that have not been adopted.
Data Operations
INSERT rows
Create a new row in the Animals table.
Create multiple rows in the Animals table.
UPDATE rows
Update a single animal record using update keyword in Animals Table
Update a multiple animal record using update keyword in Animals Table
DELETE Rows
Delete a single animal record by its ID.
Delete multiple animal records with a specific status.
Data Definition Language (DDL)
CREATE TABLE
The CREATE TABLE statement is used to create a new table in a database.
DROP TABLE
The DROP TABLE statement is used to drop an existing table in a database.
TRUNCATE TABLE
It is used to delete all the data present in the table.
ALTER TABLE
ALTER TABLE - ADD Column
ALTER TABLE - DROP COLUMN
ALTER TABLE - RENAME COLUMN
SQL Views
Create views to simplify complex queries and reuse them.
Retrieve available animals from the AvailableAnimals view.
Retrieve employee salaries from the EmployeeSalaries view.