diff --git a/SQL Deep Dive/Like Operator/questions.sql b/SQL Deep Dive/Like Operator/questions.sql index 3d9f0f7..e00829d 100644 --- a/SQL Deep Dive/Like Operator/questions.sql +++ b/SQL Deep Dive/Like Operator/questions.sql @@ -5,7 +5,9 @@ * Sample output: https://imgur.com/vXs4093 * Use EXTRACT (YEAR FROM AGE(birth_date)) we will learn about this in later parts of the course */ -SELECT ..., EXTRACT (YEAR FROM AGE(birth_date)) as "age" FROM employees; +SELECT first_name, EXTRACT (YEAR FROM AGE(birth_date)) as "age" + FROM employees + WHERE first_name LIKE 'M%'; /* @@ -14,6 +16,12 @@ SELECT ..., EXTRACT (YEAR FROM AGE(birth_date)) as "age" FROM employees; * Question: How many people's name start with A and end with R? * Expected output: 1846 */ +select COUNT(first_name) +from employees +WHERE first_name ILIKE 'A%R'; + + + /* @@ -22,7 +30,11 @@ SELECT ..., EXTRACT (YEAR FROM AGE(birth_date)) as "age" FROM employees; * Question: How many people's zipcode have a 2 in it?. * Expected output: 4211 */ +SELECT COUNT(*) +FROM customers +WHERE CAST(zip AS TEXT) LIKE '%2%'; +number of people with 2 in their zipcode is = 4211 /* @@ -31,6 +43,9 @@ SELECT ..., EXTRACT (YEAR FROM AGE(birth_date)) as "age" FROM employees; * Question: How many people's zipcode start with 2 with the 3rd character being a 1. * Expected output: 109 */ +SELECT COUNT(*) +FROM customers +where cast(zip as text) LIKE '2_1%'; /* @@ -39,5 +54,8 @@ SELECT ..., EXTRACT (YEAR FROM AGE(birth_date)) as "age" FROM employees; * Question: Which states have phone numbers starting with 302? * Replace null values with "No State" * Expected output: https://imgur.com/AVe6G4c -*/ +*/SELECT COALESCE(state, 'no state') AS State +FROM customers +WHERE phone LIKE '302%' +