NextLeap SQL Cheat Sheet Compressed
NextLeap SQL Cheat Sheet Compressed
PostgreSQL is a good place to start as it is close to standard SQL syntax and can be easily adapted to other
dialects.
2. Get all the products where price is more than $50 5. Get all the products where price is lower than $50
SELECT * SELECT *
FROM product FROM product
WHERE price > 50; WHERE price < 50;
3. Get all the products where price is exactly equal to 5. Get all the products where price is between $30 and
$50 $60
SELECT * SELECT *
FROM product FROM product
WHERE price = 50; WHERE price BETWEEN 30 AND 60;
2. Get the products from the USA and France 5. Fetch names of products that start with any letter
SELECT * followed by 'rain' (like 'train' or 'grain')
FROM product SELECT name
WHERE origin_country IN ('USA', 'France'); FROM product
WHERE name LIKE '_rain';
3. Fetch names of products that are not
watches:
SELECT name
FROM product
WHERE name != 'watch';
String Comparison
Operator Description Example
SELECT * FROM products WHERE product_name =
= Checks if two strings are equal.
'Laptop';
Checks if two strings are not SELECT * FROM products WHERE category <>
<> or !=
equal. 'Electronics';
Checks if a string matches a SELECT * FROM products WHERE product_name LIKE
LIKE
pattern using % and _. 'L%';
Checks if a string does not match SELECT * FROM products WHERE product_name NOT
NOT LIKE
a pattern. LIKE 'Sneak%';
2. DELETE: To delete data from a table, use the -- Update using subquery
UPDATE products
DELETE command
SET category_id = (SELECT category_id FROM categories
DELETE FROM category WHERE category_name = 'Electronics')
WHERE category_id = (SELECT category_id FROM categories
WHERE name IS NULL; WHERE category_name = 'Computers');
Find out what time it is: To create a date, time, or timestamp, write the value as a
SELECT CURRENT_TIME; string and cast it to the proper type.
SELECT CAST('2021-12-31' AS date);
Get today's date: SELECT CAST('15:31' AS time);
SELECT CURRENT_DATE; SELECT CAST('2021-12-31 23:59:29+02' AS timestamp);
Using ORDER BY on date and time columns sorts rows Use comparison operators <, <=, >, >=, and = to compare date
chronologically from the oldest to the most recent: and time values:
SELECT order_date, product, quantity SELECT order_date, product, quantity
FROM sales FROM sales
ORDER BY order_date; ORDER BY order_date; -- Find sales made in July 2023:
Use DESC to sort from the most recent to the oldest --Find customers who registered in July 2023:
SELECT order_date, product, quantity SELECT order_date, product_name, quantity
FROM sales FROM sales
ORDER BY order_date DESC; WHERE order_date >= '2023-07-01'
AND order_date < '2023-08-01';
Use EXTRACT() or DATEPART() to get specific parts of a Find the count of sales by month:
date: SELECT EXTRACT(YEAR FROM order_date) AS year,
SELECT EXTRACT(YEAR FROM order_date) FROM sales; EXTRACT(MONTH FROM order_date) AS month,
SELECT DATEPART(YEAR, order_date) FROM sales; -- For COUNT(*) AS count
SQL Server; FROM sales
GROUP BY year, month
NextLeap Data Analyst Fellowship ORDER BY year, month;
Date Time Functions
Function Description Example
Returns the current date (without
CURRENT_DATE SELECT CURRENT_DATE;
time) in the session's time zone.
Returns the current time (without
CURRENT_TIME SELECT CURRENT_TIME;
date) in the session's time zone.
Returns the current date and time
CURRENT_TIMESTAMP (with fractional seconds) in the SELECT CURRENT_TIMESTAMP;
session's time zone.
Returns the current date and time in
LOCALTIMESTAMP SELECT LOCALTIMESTAMP;
the session's time zone.
NOW() Returns the current date and time. SELECT NOW();
Converts a timestamp to a specified SELECT TIMEZONE('America/New_York', TIMESTAMP '2024-07-14
TIMEZONE
time zone. 12:00:00');
Returns the time zone offset of a SELECT TIMEZONE_OFFSET('America/Los_Angeles', TIMESTAMP
TIMEZONE_OFFSET
given timestamp. '2024-07-14 12:00:00');
CURRENT_TIMEZONE Returns the current time zone. SELECT CURRENT_TIMEZONE();