SELECT
(SQL DML Clause) Selects data from specified columns.
SELECT
*
FROM customers
The above query will select all columns from the customer table.
SELECT DISTINCT
(SQL DML Clause) Removes duplicates & ensures that each value only appears once in the results.
SELECT DISTINCT Country
FROM customers
Select each distinct country from the customers table. If ‘Germany’ or another country shows up more than once, it will only be selected once.
TOP
(SQL DML Clause) Used to limit your data. Restrict the number of rows.
SELECT TOP 3
FROM customers
Above query will select the top 3 customers (i.e., it will select customers with the customer id 1, 2, and 3)
FROM
(SQL DML Clause) Specifies where the data comes from (i.e., which table)
SELECT
*
FROM Customers
WHERE
(SQL DML Clause) Filters data based on a condition.
SELECT *
FROM customers
WHERE country =’Mexico’;
GROUP BY
(SQL DML Clause) Splits the result set into groups of values. Commonly used with aggregate functions.
The GROUP BY clause is used to arrange data into groups with the help of aggregate functions.
The way that it works is, if a particular column has the same values in different rows then it will amalgamate these rows into a group
Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s)
SELECT
COUNT(customerID),
Country
FROM Customers
GROUP BY Country
Output:
3 Argentina
2 Austria
2 Belgium
Group by vs Order by:
ORDER BY example:
SELECT
first_name, last_name
FROM actor
ORDER BY last_name, first_name;
Output
Christian Akroyd
Debbie Akroyd
Cuba Allen
Kim Allen
Meryl Allen
Jim Astaire
GROUP BY example:
SELECT count(film_actor.actor_id) AS num_of_films_appeared_in,
actor.actor_id,
actor.first_name,
actor.last_name
FROM actor
INNER JOIN film_actor
ON actor.actor_id = film_actor.actor_id
GROUP BY film_actor.actor_id;
Output:
2 1 Penelope Guiness
25 2 Nick Wahlberg
22 3 Ed Chase
GROUP BY & ORDER BY together:
SELECT count(film_actor.actor_id) AS num_off_films_appeared_in,
actor.actor_id,
actor.first_name,
actor.last_name
FROM actor INNER JOIN film_actor
ON actor.actor_rid = film_actor.actor_id
GROUP BY film.actor.actor_id
ORDER BY num_of_films_appeared_in DESC;
Output:
42 107 Gina Degeneres
41 102 Walter Torn
40 198 Mary Keitel
39 181 Matthew Carrey
Aggregate Functions
(SQL DML Clause) Aggregate functions include MIN(), MAX(), COUNT(), SUM(), AVG()
HAVING
(SQL DML Clause) Used to filter aggregated data. *Can only use the HAVING function with the GROUP BY clause.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s)
SELECT
COUNT(CustomerID),
Country
FROM Customers
GROUP BY Country
HAVING COUNT(Customer_ID) > 5
Output:
9 Brazil
11 France
11 Germany
ORDER BY
(SQL DML Clause) Sorts data. Use either ASC (lowest to highest value) or DESC (highest to lowest value). If you don’t specify ASC or DESC, ASC will be used; ASC is the default.
SELECT
*
FROM Products
ORDER BY Price
Output:
product_id product_name supplier_id category_id unit price
33 Geitost 15 4 500g 2.5
Group by vs Order by
ORDER BY example:
SELECT
first_name, last_name
FROM actor
ORDER BY last_name, first_name;
Output
Christian Akroyd
Debbie Akroyd
Cuba Allen
Kim Allen
Meryl Allen
Jim Astaire
GROUP BY example:
SELECT count(film_actor.actor_id) AS num_of_films_appeared_in,
actor.actor_id,
actor.first_name,
actor.last_name
FROM actor
INNER JOIN film_actor
ON actor.actor_id = film_actor.actor_id
GROUP BY film_actor.actor_id;
Output:
2 1 Penelope Guiness
25 2 Nick Wahlberg
22 3 Ed Chase
GROUP BY & ORDER BY together:
SELECT count(film_actor.actor_id) AS num_off_films_appeared_in,
actor.actor_id,
actor.first_name,
actor.last_name
FROM actor INNER JOIN film_actor
ON actor.actor_rid = film_actor.actor_id
GROUP BY film.actor.actor_id
ORDER BY num_of_films_appeared_in DESC;
Output:
42 107 Gina Degeneres
41 102 Walter Torn
40 198 Mary Keitel
39 181 Matthew Carrey
COUNT
(SQL DML Clause) Count the total number of something.
SELECT
COUNT(*)
FROM gold.dim_customers
Output:
18484
SELECT
*
FROM gold.dim_customers
Output:
All info from table (18484 rows, so 18484 customers IF each row represents one customer)
SELECT COUNT(DISTINCT (customerID) AS unique_customers
FROM gold.dim_customers
Output
Unique Customers
18484
SQL DML Clauses in Execute Order
1) FROM – Identify source tables and joins
2) WHERE – Filter individual rows (raw data)
3) GROUP BY – Put the rows in buckets
4) Aggregate functions – Calculate aggregations (SUM, COUNT, AVG, MIN, & MAX)
5) HAVING – Filter groups based on aggregated results
6) SELECT – Return chosen columns or expressions (can now include aggregates)
7) DISTINCT – Remove duplicate rows from the final result set.
8) ORDER BY – Sort the results
9) TOP/LIMIT – Return only a subset of rows
SQL DML Commands
INSERT
UPDATE
DELETE
TRUNCATE
INSERT
(SQL DML Command)
Manipulate data (i.e., insert new rows or update rows).
2 Methods:
1) Manually
2) Use another table
Syntax:
1) Manually:
INSERT INTO table_name (col1, col2, col3, etc.)
VALUES (value1, value2, value3, etc.)
Using another table:
INSERT INTO destination_table (col1, col2, col3, etc.)
SELECT col1, col2, etc.
FROM source_table
Examples:
Manually:
INSERT INTO Customers (customer_name, contact_name, address, city, postal_code, country)
VALUES (‘cardinal’, ‘Tom B. Erichsen’, ‘871 Stanley St.’, ‘Frederick’, ‘20987’, ‘USA’)
Using another table:
INSERT INTO High_Value_Orders (order_id, customer_id, order_total)
SELECT order_id, customer_id, total_amount
FROM All_Orders
INSERT INTO New_Customers
SELECT
*
FROM Old_Customers
UPDATE
(SQL DML Command)
Change the content of already existing rows
Syntax:
UPDATE table_name
SET col1 = value1, col2 = value2, etc.
WHERE condition;
Example:
UPDATE Customers
SET contact_name = ‘Alfred Schmidt’, City = ‘Frankfurt’
WHERE customer_id = 1
DELETE
(SQL DML Command)
Remove already existing row(s) inside your table.
Syntax:
DELETE
FROM table_name
WHERE condition
Example:
DELETE
FROM Customers
WHERE customer_name = ‘Alfred Futterkiste’
TRUNCATE
(SQL DML Command)
Use if you want to delete EVERYTHING from a table.
Clears all records from a table without removing its structure.
TRUNCATE is faster than DELETE, so use TRUNCATE when you’re working with large tables.
Example:
TRUNCATE TABLE Categories
This will delete the table categories
TRUNCATE TABLE users
Empties the users table while keeping its schema intact.
AND
(SQL Logical Operator)
All conditions must be true
Syntax:
SELECT col1, col2, col3, etc.
FROM table_name
WHERE condition1 AND condition 2 AND condition3, etc.
Example:
SELECT
*
FROM Customers
WHERE country = ‘Spain’ AND customer_name LIKE ‘G%’
OR
(SQL Logical Operator)
At least one condition must be true
Syntax:
SELECT col1, col2, etc.
FROM table_name
WHERE condition1 OR condition2 OR condition3
Example:
SELECT
*
FROM Customers
WHERE country = ‘Germany’ OR country = ‘Spain’;
NOT
(SQL Logical Operator)
Excludes matching values
Syntax:
SELECT col1, col2, etc.
FROM table_name
WHERE NOT condition;
Example:
SELECT
*
FROM Customers
WHERE NOT country = ‘Spain’;
LIKE
(SQL Logical Operator)
Syntax:
SELECT col1, col2
FROM table_name
WHERE col LIKE pattern;
Example 1: Pull all records from Customer table where the customer’s name starts with ‘a’.
SELECT
*
FROM Customers
WHERE customer_name LIKE ‘a%’;
Example 2: Pull all records from the Customer table where where the city contains the letter ‘n’.
SELECT
*
FROM Customers
WHERE city LIKE ‘%L%’;
Example 3: Pull all records from the Customer table where the customer’s name ends with an ‘a’
SELECT
*
FROM Customers
WHERE CustomerName LIKE ‘%a’
Example 4: Return all customers that start with ‘La’
SELECT
*
FROM Customers
WHERE CustomerName LIKE ‘La%’;
BETWEEN
(SQL Logical Operator)
Checks if a value is within a range.
Syntax:
SELECT column_name
FROM table_name
WHERE column_name BETWEEN value1 AND value2
Example:
SELECT
*
FROM Products
WHERE Price BETWEEN 10 AND 20
IN
(SQL Logical Operator)
Syntax:
SELECT column_name
FROM table_name
WHERE column_name IN (value1, value2, etc.);
Example:
SELECT
*
FROM Customers
WHERE country IN (‘Germany’, ‘France’, ‘UK’);
CREATE
(SQL DDL Command)
Defines new database objects like tables, indexes, or entire databases.
-CREATE DATABASE my_database: Creates a new database
-CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR (50), email VARCHAR (100): Defines a table with columns and data types.
-CREATE INDEX idx_email ON users (email): Generates an index for faster lookups.
Note: When making a new object, it will be empty (e.g., if you make a new table, it will be an empty table.)
Syntax:
CREATE TABLE table_name (
col1 datatype,
col2 datatype,
col3 datatype,
etc.
);
Example:
CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
ALTER
SQL DDL Command. Modifies existing database structures without affecting stored data.
So, it can edit the table (i.e., add a column, change the data type, etc.)
Syntax:
ALTER TABLE table_name
ADD column_name datatype;
Example:
ALTER TABLE Customers
ADD Email varchar(255);