Link: SQL
Filter strings
- When filter by string names, use single quote
- double quote is reserved for referencing column names
SELECT * FROM reviews WHERE manufacturer = 'Biogen'
Combine WHERE with AND, OR, NOT
- When using multiple
AND
,OR
, do not add comma between conditions! - Use
()
bracket to prioritize and group the conditions
SELECT * FROM reviews
WHERE condition1
AND/OR condition2
AND/OR condition3;
Combine WHERE with NOT
SELECT * FROM reviews
WHERE
stars NOT BETWEEN 2 AND 4
AND NOT stars = 5;
Combine WHERE with BETWEEN
WHERE...BETWEEN x AND y
- In SQL,
BETWEEN
is inclusive (close bracket)!
SELECT * FROM reviews
WHERE year BETWEEN 2000 AND 2010;
Combine WHERE with IN
WHERE ... IN ()
SELECT drug, manufacturer, units_sold
FROM pharmacy_sales
WHERE manufacturer IN ('Biogen', 'Bayer', 'Eli Lilly');
Combine WHERE with LIKE
WHERE ... LIKE '...'
WHERE ... NOT LIKE '...'
- Often use with wildcard
%
0 or multiple chars) and_
(single character)
SELECT product_id,
manufacturer,
drug
FROM pharmacy_sales
WHERE drug LIKE '%Relief%';