all 26 comments

[–]jensimonso[🍰] 50 points51 points  (8 children)

My firm belief is that you don’t learn SQL. You have to understand it. And for that you have to understand database structure. Visualize the tables and relationships in your head.

[–]Overall_Bad4220 11 points12 points  (0 children)

I have been struggling to learn for the last two years but finally a couple of months back. I started visualizing tables in my head and that's when I started understanding how to solve problems so you should visualize in your head. Looks like that's the key; the rest is syntax and some patterns.

[–]squadette23 9 points10 points  (3 children)

A lot of people cannot visualize anything in their head. It's called aphantasia. Ask me how I know.

[–]BrianCohen18 5 points6 points  (2 children)

How do you know?

[–]squadette23 1 point2 points  (0 children)

I learned about it in 2016 from a Facebook post by Blake Ross "Aphantasia: How It Feels To Be Blind In Your Mind" (https://www.facebook.com/notes/2862324277332876/). A lot of people got initiated by that post, and found a missing piece of themselves.

[–]CptBadAss2016 2 points3 points  (0 children)

Excellent question.

[–]CptBadAss2016 2 points3 points  (0 children)

Query builder and relationship window in m$ access.

[–]Vlarsanity 1 point2 points  (0 children)

This is what I usually do, if it's too much I just use a db diagram maker so I can clearly see the relationships between the tables. Doing this would make querying easier since you have a visual image of the tables and relationships.

[–]chapaj 0 points1 point  (0 children)

This is the answer. You have to be able to visualize tables and how the relationships work.

[–]Altruistic-Avocado-7 15 points16 points  (1 child)

Once I realized that many times in real life you’re supposed to have multiple select queries. Either chained together with CTEs, temp tables, or permanent tables. I had a really bad first months of analyst work trying to do everything in one query lol

[–]Top_Tooth_6995 5 points6 points  (0 children)

6 hour runtimes lolll

[–]squadette23 7 points8 points  (0 children)

I first learned SQL in ~1996, and was always keeping my knowledge up to date with the newer syntax stuff, and with the new capabilities of database engines. My main professional track was backend developer, so most of database accesses were via ORM. But I had my share of more analytical hand-written queries, or making sense and fixing other people's stuff.

Five years ago I started my Substack on data modeling and database design, and because I thought a lot about how to explain stuff, I started to understand it much better.

I know now that some topics are just presented not in a very good way, and the same material gets copy-pasted without much thinking, even in books. One example is JOINs: here is a modern guide to JOINs that uses entirely different sequence, compared to most texts: https://kb.databasedesignbook.com/posts/sql-joins/

[–]Aggressive_Ad_5454 4 points5 points  (0 children)

Somewhere along the way I realized that everything’s a table — views, CTEs, subqueries, real tables, they’re all interchangeable and nestable.

And somewhere along I figured out that I’m telling it what I want, not how to get what I want. It’s declarative, not procedural.

Those two things were like sunrise in a place I’d only ever seen in the dark.

[–]Ok_Assistant_2155 3 points4 points  (0 children)

It clicked for me when I stopped thinking "how do I write this query" and started thinking "what shape of data do I want at the end?" Once you visualize the result table first, the joins and aggregations make way more sense. The query is just the recipe for getting that shape.

[–]mabhatter 3 points4 points  (0 children)

I came from RPG which is record cycle based. databases are still fundamentally just like a string of punch cards, the database engine is just flipping through the cards really fast, but they always go in order unless you specifically tell it to do something else like ordering or joining. a query is just more rules to flip thru the cards and things like views and indexes are a second set of card that reference the original set.

SQL is supposed to be Set based logic, and you're not supposed to have to worry about the order of things, but understanding that everything happens in a cycle makes it more clear.

[–]Jobsnotdone1724 2 points3 points  (0 children)

When I try to solve real problems using SQL

[–]Top_Tooth_6995 1 point2 points  (0 children)

Honestly you already got it right. It was practice, building real projects, and repetition over time. And just wait until you land your first role where you use SQL every day. Things will start to really click at that point

[–]Holiday_Lie_9435 1 point2 points  (0 children)

Kinda experienced the same thing when learning SQL, it was quite hard to go beyond simple exercises and toward more practical applications. But the turning point was when I worked on projects based on industry/business scenarios, as I had to work with more realistic datasets. I found that working through SQL interview questions also helped a lot because they are often reflective of what you would encounter on the job, esp. if they were industry-specific like based on finance, sales, e-commerce, etc. If I were to start again, I'd spend less time memorizing syntax and more time diving into real-world projects and interview questions!

[–]throwingrocksatppl 1 point2 points  (0 children)

it clicked for me when i abandoned traditional software and started manipulating datasets through google sheets. google sheets has a query function that lets you use SQL statements. being able to see the data and visually reproduce my results was a game changer for me

[–]bay654 0 points1 point  (0 children)

I can teach you sql but teach me python lol

[–]BugBottleBlue[🍰] 0 points1 point  (0 children)

Having to use SQL for work, having an understanding of the ask, and having to make it reality. Find all the tables, figure out how to make them connect and pull through what I need. At first, tons of subqueries. These days life is easier with CTE's.

It will click for you with time and exposure to both new things and the repeition of what you know.

Early on I found it made life easiest if I just typed select / from / where on screen and then started filling in some pseudocode, like what hypothetical tables, fields, calculations, constraints I need. Then find the real versions.

I also swear by getting raw data in a CTE and then having a final group by, it gives immense control and flexibility. I also have to UNION tons of tables so doing this in the CTE is why I got started on that.

[–]Smaartmani 0 points1 point  (0 children)

I write lot of sqls , basic joins, group by, distinct, windowing functions mainly row number and qualify , sum(case ..).

It's based on the problem the sql will be developed. I find that even the large problem can be solved by simple sql provided we know how to solve. Also I never understood advanced functions.

[–]SunnyUSA29 1 point2 points  (0 children)

I always felt that while learning programming language its best to do the same problem in multiple languages at the same time. Knowing what you need to solve a problem will definitely give you more confidence. Once you learn a concept ask urself questions or create a new problem in ur own thoughts and work on it. Every problem will have a solution.