SQL Handbook - Cracknontech
SQL Handbook - Cracknontech
SQL
SQL, or Structured Query Language, is a way we talk to databases.
It's like a tool that helps us ask for specific information and put
together detailed reports. It's used everywhere in technology when
we're dealing with data. For Product Managers, SQL skills are a
must-have as they will regularly be working with data.
SELECT
Fetch the id and name columns from the product table:
SELECT id, name
FROM product;
Fetch names of products that start with any letter followed by 'rain'
(like 'train' or 'grain'):
SELECT name
FROM product
WHERE name LIKE '_rain';
AGGREGATE FUNCTIONS
Count the number of products:
SELECT COUNT(*)
FROM product;
Find the average price of products for each category whose average
is above 3.0:
SELECT category, AVG(price)
FROM product
GROUP BY
category
HAVING AVG(price) > 3.0;
ORDER BY
Fetch product names sorted by the price column in the
default ASCending order:
SELECT name
FROM
product
ORDER BY price [ASC];
COMPUTATIONS
Use +, -, *, / to do basic math. To get the number of seconds in a
week:
SELECT 60 * 60 * 24 * 7;
-- result: 604800
ROUNDING NUMBERS
Round a number to its nearest integer:
SELECT ROUND(1234.56789);
-- result: 1235
DIVISION BY 0
To avoid this error, make sure the denominator is not 0. You may use
the NULLIF() function to replace 0 with a NULL, which results in
a NULL for the entire expression:
count / NULLIF(count_all, 0)
JOIN
JOIN is used to fetch data from multiple tables. To get the names of
products purchased in each order, use:
SELECT
orders.order_date,
product.name AS
product, amount
FROM orders
JOIN
product
ON product.id = orders.product_id;
INSERT
To insert data into a table, use the INSERT command:
INSERT INTO category
VALUES
(1, 'Home and Kitchen'),
(2, 'Clothing and Apparel');
You may specify the columns to which the data is added. The
remaining columns are filled with predefined default values or
NULLs.
INSERT INTO category (name)
VALUES ('Electronics');
UPDATE
To update the data in a table, use the UPDATE command:
UPDATE category
SET
is_active = true,
name = 'Office'
WHERE name = 'Ofice';
DELETE
To delete data from a table, use the DELETE command:
DELETE FROM category
WHERE name IS NULL;
DATE AND TIME
There are 3 main time-related types: date, time, and
timestamp. Time is expressed using a 24-hour clock, and it can be
as vague as just hour and minutes (e.g., 15:30 – 3:30 p.m.) or as
precise as microseconds and time zone (as shown below):
2021-12-31 14:39:53.662522-05
date time
timestamp
YYYY-mm-dd HH:MM:SS.ssssss±TZ
14:39:53.662522-05 is almost 2:40 p.m. CDT (e.g., in Chicago;
in UTC it'd be 7:40 p.m.). The letters in the above example
represent:
In the date part: In the time part:
YYYY – the 4-digit year. HH – the zero-padded
mm – the zero-padded month hour in a 24-hour clock.
MM – the minutes.
(01—January through SS – the seconds.
12—December). Omissible. ssssss – the
dd – the zero-padded day. smaller parts of a second
– they can be
expressed using 1 to
6 digits.
Omissible.
±TZ – the timezone. It
must start with either + or
-, and use two digits
relative to UTC.
Omissible.
CURRENT DATE AND TIME
Find out what time it is:
SELECT CURRENT_TIME;
Use the DESCending order to sort from the most recent to the oldest:
SELECT order_date, product,
quantity FROM sales
ORDER BY order_date DESC;
COMPARING DATE AND TIME
VALUES
You may use the comparison operators <, <=, >, >=, and =
to compare date and time values. Earlier dates are less than
later ones. For example, 2023-07-05 is "less" than
2023-08-05.
Note: Pay attention to the end date in the query. The upper bound
'2023-08-01' is not included in the range. The timestamp
'2023- 08-01' is actually the timestamp '2023-08-01
00:00:00.0'. The comparison operator < is used to ensure the
selection is made for all timestamps less than '2023-08-01
00:00:00.0', that is, all timestamps in July 2023, even those
close to the midnight of August 1, 2023
EXTRACTING PARTS OF DATES
The standard SQL syntax to get a part of a date
is SELECT EXTRACT(YEAR FROM
order_date) FROM sales;
2023 2 69
Note that you must group by both the year and the month.
EXTRACT(MONTH FROM order_date) only extracts the month
number (1, 2, ..., 12). To distinguish between months from different
years, you must also group by year.
CASE WHEN
CASE WHEN lets you pass conditions (as in the WHERE clause),
evaluates them in order, then returns the value for the first condition
met.
SELECT
name
,
CASE
WHEN price > 150 THEN 'Premium'
WHEN price > 100 THEN
'Mid-range' ELSE 'Standard'
END AS price_category
FROM product;
Here, all products with prices above 150 get the Premium label,
those with prices above 100 (and below 150) get the Mid-range
label, and the rest receive the Standard label.
CASE WHEN and GROUP BY
You may combine CASE WHEN and GROUP BY to compute
object statistics in the categories you define.
SELEC
T
CASE
WHEN price > 150 THEN 'Premium'
WHEN price > 100 THEN
'Mid-range' ELSE 'Standard'
END AS price_category,
COUNT(*) AS products
FROM product
GROUP BY price_category;
Count the number of large orders for each customer using CASE WHEN
and SUM():
SELECT
customer_id,
SUM(
CASE WHEN quantity > 10
THEN 1 ELSE 0 END
) AS large_orders
FROM sales
GROUP BY customer_id;
RANKING FUNCTIONS
RANK – gives the same rank for tied values, leaves gaps.
DENSE_RANK – gives the same rank for tied values without
gaps. ROW_NUMBER – gives consecutive numbers without
gaps.
T-Shirt 7 4 7
RUNNING TOTAL
A running total is the cumulative sum of a given value and all preceding
values in a column.
SELECT date, amount,
SUM(amount) OVER(ORDER BY date)
AS running_total
FROM sales;
MOVING AVERAGE
A moving average (a.k.a. rolling average, running average) is a
technique for analyzing trends in time series data. It is the average
of the current value and a specified number of preceding values.
SELECT date, price,
AVG(price) OVER(
ORDER BY date
ROWS BETWEEN 2
PRECEDING AND CURRENT
ROW
) AS moving_averge
FROM stock_prices;