Finished the Intermediate part of SQL but have some doubts!


#1

Hey guys,
I’ve just finished the intermediate part of the SQL course and everything was fine until I got to the Cumulative Project! I could not understand the syntax from exercises 6, 11, 12 and 13. To be more specific, I cracked my head to solve them but it just didn’t! Should I relax a little and go on or go back the whole path?
This code below is one I could not understand. I mean, the first part when it’s asking for the Name, Address and Phone is fine, but when it goes further and THIS:
(SELECT Borderline_High, High FROM recommended_values WHERE Lipid = “LDL”) rv — what’s “rv”?

SELECT
active_patients.Name AS Name,
round(LDL) AS LDL,
active_patients.Address AS Address,
active_patients.Phone AS Phone
FROM
panels,
(SELECT Borderline_High, High FROM recommended_values WHERE Lipid = “LDL”) rv
JOIN active_patients ON panels.patient_ID = active_patients.ID
GROUP BY
LDL,
active_patients.Name
HAVING LDL >= rv.Borderline_High
ORDER BY LDL DESC;

This is another example:
SELECT
COUNT(CASE WHEN (LDL >= rv.Borderline_High AND LDL < rv.High) THEN LDL END) AS Borderline_High,
COUNT(CASE WHEN LDL >= rv.High THEN LDL END) AS High
FROM
panels,
(SELECT Borderline_High, High FROM recommended_values WHERE Lipid = “LDL”) rv;

I’m still getting used to the CASE statement so it’s a little tricky. But again the “rv” which I have no idea what stands for.

I would really appreciate if someone could help a brother out.

Thanks!

https://www.codecademy.com/paths/data-science/tracks/sql-intermediate/modules/dspath-sql-cumulative-project/projects/cadcademy-milestone :slight_smile:


#2

“rv” is the name for the data that this SQL statement returns: (SELECT Borderline_High, High FROM recommended_values WHERE Lipid = “LDL”)
It’s the same as “active_patients.Phone AS Phone”, just for a collection instead of a column.
You can also write it like this:
(SELECT Borderline_High, High FROM recommended_values WHERE Lipid = “LDL”) AS rv
This can then be used to reference the columns returned by this statement like this:
rv.Borderline_High