In Lecture 13, the solution involved performing UNION on three queries on the customers table.
– Create a report on the customers table showing the
– loyalty type based on the points each customer has.
– points < 2000: Bronze
– points >= 2000 and <= 3000: Silver
– points > 3000: Gold
– Note that the ORDER BY clause should appear in the final
– query only. If placed elsewhere, a syntax error will result
USE sql_store;
SELECT
CONCAT(c.first_name, " ", c.last_name) AS customer,
"Bronze" as type
FROM
customers c
WHERE
c.points < 2000
UNION
SELECT
CONCAT(c.first_name, " ", c.last_name) AS customer,
"Silver" as type
FROM
customers c
WHERE
c.points BETWEEN 2000 AND 3000
UNION
SELECT
CONCAT(c.first_name, " ", c.last_name) AS customer,
"Gold" as type
FROM
customers c
WHERE
c.points > 3000
ORDER BY
customer;
-- I wonder if we can generate the same report using a
-- CASE statement?
SELECT
CONCAT(c.first_name, " ", c.last_name) AS customer,
CASE
WHEN c.points < 2000 THEN "Bronze"
WHEN c.points BETWEEN 2000 AND 3000 THEN "Silver"
ELSE "Gold"
END as type
FROM
customers c
ORDER BY
customer;
Although the CASE statement hasn’t yet been covered, I remember learning about it from a different course on T-SQL. I got the same results as the UNION queries.
Is the query using CASE more efficient than performing UNION on three separate queries?
Thank you kindly.