Did you know you can list your tables as aliases in SQL?
Say you have a table called "quiz" with unique user_id's.
You could list your table as "q" (or whatever you like) and call its user_id column in the following way:
SELECT q.user_id
FROM quiz q
LIMIT 10;
Did you see that? Where you place your table name ("quiz"), you can place your variable name after it, i.e. "q".
This tidies up your code a little bit. Say you had two other columns called "question" and "answer" - you could select the three columns by using the code:
SELECT q.user_id, q.question, q.answer
FROM quiz q
LIMIT 10;
Below is some example code from Codecademy's Funnels excercises on their Data Science course:
SELECT q.user_id,
h.user_id IS NOT NULL AS 'is_home_try_on',
h.number_of_pairs,
p.user_id IS NOT NULL AS 'is_purchase'
FROM quiz q
LEFT JOIN home_try_on h
ON q.user_id = h.user_id
LEFT JOIN purchase p
ON p.user_id = q.user_id
LIMIT 10;
Here is a link to the W3 School's examples of how to create SQL aliases: https://www.w3schools.com/sql/sql_alias.asp
No comments:
Post a Comment