SQL - where did "rv" come from


#1

I’m new to SQL (and programming). Can someone explain to me what “rv” stands for in this block of code? I’m guessing it’s a short hand for the table named recommended_values but no where did it rename the table to rv. It just seems to randomly appear in the code. This one of the learning module solutions I’m working on and I hate to move on b/c I have no idea how they came up with the short hand rv. Thank you!!!

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
HAVING LDL>= rv.Borderline_High
ORDER BY LDL DESC;


#2

Hi @msphilomath,

See if this helps you out please: Tables Aliases

The short is, as the title to the other post suggests; a Table Alias. SQL doesn’t not like knowing where it’s getting it’s info from. So every object needs a name. This object is a derived table, and needs a name in order for SQL to reference it correctly.


#3

Am I the only one thinking this particular exercise was of a whole other level of complexity with respect to the SQL exercise done previously? It’s like they passed from basic SQL queries to nested commands and new concepts (like this table aliases) out of the blue.


#4

Totally agree and totally confused by this project. Not sure what the point of having a project where you’re being asked to do things you’ve never done before and with no help other than to just give you the whole code.


#5

Working on this same project, and fully agree that it was too much of a jump from previous lessons/exercises. What I’m not following is why my code returns different results from the suggested code - any help is appreciated

My code:

select active_patients.name as ‘Name’,
round(panels.ldl) as ‘LDL’,
active_patients.address as ‘Address’,
active_patients.phone as ‘Phone’
from panels, active_patients
where LDL >= ‘130’
group by name
order by LDL desc
limit 5;

Results:

|Name|LDL|Address|Phone|
|Alfredo Haggis|136.0|9 Roth Terrace|596-731-3727|
|Anastasia Bartolomieu|136.0|57 Fallview Street|326-441-6684|
|Antonella Horsell|136.0|19 Roth Road|380-427-0552|
|Ariel Gooday|136.0|2836 Harbort Drive|292-741-8754|
|Bari Arnely|136.0|860 Sycamore Way|692-377-4787|

their code is returning results w/ LDL of 199, why are mine cut off at 136? brand new to sql


#6

I think you need to use the table connection, otherwise the data will be incomplete.


#7

For those who are still struggling to understand how the rv alias is working, it is just an alternative to using WITH alias AS ().

So, this code:

is exactly the same as this:

WITH rv AS (
SELECT Borderline_High, High
FROM recommended_values
WHERE Lipid = ‘LDL’)
SELECT
active_patients.Name AS ‘Name’,
round(panels.LDL) AS ‘LDL’,
active_patients.Address AS ‘Address’,
active_patients.Phone AS ‘Phone’
FROM
panels, rv

Why they decided to include that as the hint without explaining that it works this way is beyond me, but if you watch the video (made for a previous version of the exercise) it is completed using WITH AS.