Hey there, SQL-ites. Time for another (yes, another!) PostgreSQL skills challenge. It’s our last stop before moving into more intermediate and advanced PostgreSQL skills, such as JOINS.
If you’re just joining this series on SQL, we previously covered the HAVING statement & GROUP BY. Here’s the home page of our journey in learning PostgreSQL. Today, we do a more rounded knowledge check of older PostgreSQL statements, such as SELECT WHERE, COUNT & ORDER BY.
We have 3 problems, and provide the winning code for each, based on our sample database. In the past, I’ve given more explanation to the problems, but because I’m trying to get some traction myself in moving forward, we’ll only have problem & solution laid out here.
Alright, let’s go!
1. Return the customer IDs of customers who have spent at least $110 with the staff member who has an ID of 2.
The answer should be customers 187 and 148.
SELECT customer_id, SUM(amount)
GROUP BY customer_id
HAVING SUM(amount) >110;
2. How many films begin with the letter J?
The answer should be 20.
SELECT COUNT(*) FROM film
WHERE title LIKE ‘J%’;
3. What customer has the highest customer ID number whose name starts with an ‘E’ and has an address ID lower than 500?
The answer is Eddie Tomlin.
SELECT customer_id, first_name, last_name, address_id
WHERE address_id <500 AND first_name LIKE ‘E%’
ORDER BY customer_id DESC;
We’ll see you on the other side soon, for some JOINS awesomeness. 🙂