r/SQL • u/StickWarm9316 • 13d ago
SQLite Help with Harvard CS50 Subquery and Joins
Due to an unfortunate but also fortunate promotion at work, I now have to learn SQL. I had some familiarity with looking at it before, but never writing it and my understanding of it is basically null
So, enter Harvard CS50 Subquery with SQLite, get through week 0 in about 3 hours, single query gave me basically no trouble
Enter week 1, and I've spent over 12 hours trying to figure out what the hell is going on. The course material basically just shows one or two examples of subquery, and very modest examples of joining, then the course practice questions are combining different concepts and way more advanced and my brain hurts like hell. I'm really having trouble with subquery and joins both, I think I'm taking them too literally, but the course material I have really isn't helping, I had ChatGPT write me 100 practice questions, and I got through 50 just fine, then as soon as I hit subquery and grouping, it's like I hit a wall.
Can someone help explain this to me? I think I'm just failing to understand the architecture, the syntax kind of makes sense to me, I need someone to break down subquery and joins in... human language
Update:
Thanks to everyone here I was actually able to figure it out, I have only been learning sql for 5 days, but was able to complete all of the problem sets with some dedicated research and practice, I actually feel like now I am quite good with joins and subquery
Thank you everyone :D
5
u/McBurgerQueen 13d ago
Can you share a problem you’re struggling with
1
u/Far_Swordfish5729 13d ago
Start here. https://ptgmedia.pearsoncmg.com/images/9780735685048/samplepages/9780735685048.pdf
Best explanation I’ve read. Sql lite will use a limit clause instead of top and apply is sql server specific.
I’ve explained this in this forum several times and others have written good explanations as well. Let us know what doesn’t make sense.
1
u/Hour-Measurement-835 13d ago
Build the query in the order the database actually runs it, and look at the rows after every step. SQLite runs FROM and the joins first, then WHERE, then GROUP BY, and SELECT comes last even though you write it first. That's a lot of why it reads backwards.
So start with just the FROM and the JOIN, with SELECT * so every column shows. A join glues each row on one side to every row that matches on the other, so if a customer has 3 orders, that customer is now on 3 rows. Stare at that before you go any further. When a grouping question gives you a count that's too high, it's almost always because the join quietly multiplied the rows and nobody looked.
Add the WHERE and run it again. Only then add GROUP BY. Think of it as sorting the rows you can already see into piles, one pile per value, and COUNT or SUM squashes each pile down to one line.
Subqueries are the same habit. Copy whatever's inside the brackets, run it on its own, and see what comes back. If it's a list of ids, the outer query is just using that list like you'd typed it in.
1
u/mirinai_ai 13d ago
For joins in general, it helps to think of a join as building one wider table first. Everything you already know (WHERE, GROUP BY, HAVING, ORDER BY) then runs on that wider table as if it were a single table. The ON line is the only new part. It says which row on one side lines up with which row on the other, usually one table's id matched to the other table's ..._id column.
A subquery is similar: the inner query runs first and hands back a value or a list, and the outer query uses it as if you had typed it in by hand. Running the inner query on its own first, to see what it returns, takes a lot of the mystery out of them.
1
u/ontirius 12d ago
cs50 goes from one tiny example to a problem set that needs three concepts at once, that wall is normal. at work i almost never write nested subqueries, i write them as CTEs (with x as (...)) so every block has a name and i can run it alone and check the rows. nested or CTE is the same query, one is just readable. the ones worth learning separately are correlated subqueries, they run once per outer row and thats where the confusion actually lives
1
u/Discordbothost 12d ago
Try drawing two customers and three orders on paper. For each order, copy its matching customer's columns beside it. That's the joined table your WHERE and GROUP BY work on. Predict those three rows first; it separates understanding the join from learning the syntax.
1
u/squadette23 12d ago
You may be interested in: https://kb.databasedesignbook.com/posts/systematic-design-of-join-queries/ and its prerequisites (see "A modern guide to SQL JOINs")
1
u/nicopassaglia 1d ago
Late to this, and glad you got through it. For anyone who lands here stuck on the same week, the framing that tends to unlock it:
A JOIN makes one wider table. It glues each row of one table to the rows of another that match on a key, and then you query that wide table like any other. If a customer has three orders, they show up three times. Most join confusion is forgetting that.
A subquery is just a query whose answer you use somewhere. It comes in three shapes, and it helps to ask which one you need:
- one value: WHERE amount > (SELECT AVG(amount) FROM orders)
- a list: WHERE id IN (SELECT customer_id FROM orders WHERE ...)
- a table: FROM (SELECT ...) or a WITH block
Read them inside out, and run the inner query on its own first. If you can't say in a sentence what the inner one returns, the outer one won't make sense either.
The next wall after this is usually NULLs: NOT IN with a NULL in the list returns nothing, with no error. Worth ten minutes before it bites you at work.
If more practice helps: I built a free set of short drills for exactly these two topics, in the browser on SQLite, no sign-up. Joins: https://getsheetdrill.com/sql/lessons/3 and subqueries: https://getsheetdrill.com/sql/lessons/4. Disclosure, it's my project.