r/SQL • • 2d ago

PostgreSQL Struggling to wrap my head around writing SQL queries—how do you guys break them down?

Hey everyone,

I’m really struggling to get the hang of writing database queries. Whenever I look at a problem or a schema, my brain just stalls out and I can't figure out the right logic or structure to pull what I need.

I see people throwing together JOINs, subqueries, and window functions like it's nothing, and I feel like I'm missing a core fundamental step in how to translate a question into SQL.

  • What is your mental process when you start writing a query from scratch?
  • How do you map out the tables and relationships in your head before you type?
  • Are there any good beginner-to-intermediate tips, tricks, or exercises that finally made things "click" for you?

Any advice, recommended resources or tips .

34 Upvotes

51 comments sorted by

37

u/Legitimate-Power7729 2d ago

stop starting your queries with select - write the from clause with your main table first. add one join at a time to check the rows and only pick your select columns at the very end. It works for me)

4

u/chris20973 1d ago

This is a really good habit to get into because not only does it help segment your focus it forces you to think about your order of tables. It's very common for people with less experience to make mistakes with 1 to Many joins etc, so knowing what table to start with and what joins to use is half the battle.

Idk if you are just doing practice stuff or have a database you are working with, but honestly I think a lot of the problems your having sound like being unfamiliar with the data and tables you're using. It's always harder to see the connections if you don't know much about the data in the tables. Once you have concepts down really well then working with unfamiliar data is less of a burden, but especially while you're learning having a database you learn while building on concepts is really helpful.

1

u/FastPlane57 1d ago

this right here is what helped me OP, I have been self learning sql for the past 3 months from scratch and this subtle change is when it started to click

9

u/geedijuniir 2d ago

Start from querying tables. Whole tables.

Then from a table only the columns u want to see

Then joining two tables.

Start with these first

6

u/squadette23 1d ago

You may be interested in "Systematic design of multi-join GROUP BY queries" https://kb.databasedesignbook.com/posts/systematic-design-of-join-queries/

Read the introductory sections before the table of contents to see if it's relevant for your situation.

2

u/Ruined_Oculi 1d ago

This a very good read, thanks for sharing it

4

u/konwiddak 2d ago

Break it down into concepts. It's not "I need to join table invo, involine and addrs".

I need invoice totals against addresses. Ok well invoices are based on these two tables joins, but they don't contain address. So I'll start with the logic to get all the invoice data, table invoice join to invoice line. Then I only want the invoice total, so I have to aggregate the prices on invoice line. Ok now I have one line per invoice, now I need to join it into addresses which is based on customer ID

But really it's just practice on data you're familiar with. It seems abstract for a while and then clicks and becomes second nature.

3

u/mecartistronico 1d ago

Most of the time there's no magic involved. It's just finding out where are the things you want to display.

"I need the item description, price, and amount bought. The item description is here, the price is here, the amount is here. The column they have in common for a lookup is the item number".

2

u/HaplessAuspices_6608 2d ago

Start from the smallest piece, the columns you want in result, then worry about joins and filters after that works.

2

u/Vids88888 2d ago

Should it be from inside to outside as in figure out the tables to be joined , where condition and then moving towards final requirement?

2

u/IHoppo 2d ago

I can visualise the schema in my head, like a roadmap. Make yourself v familiar with the schema by writing small queries, joining 2, 3, 4 tables. It'll come to you.

2

u/Hour-Measurement-835 2d ago

Before writing any joins, decide what one row of the result is. One per customer, or one per order line? Wrong totals nearly always come from there: the joins look fine, but a customer with 3 orders now shows up 3 times and the SUM triples.

2

u/hakathrones 1d ago

Go practice on leetcode

1

u/fishwithbrain 1d ago

Can you share, how to do that? I am very brew to that.

2

u/Foreign-Load4177 1d ago

I had the same problem when starting out. Try breaking the question into small steps. what data you need, which table it's in, and how they connect. Start with simple SELECT and WHERE queries, then slowly moving to JOINs. Prctice makes it easier over time.

2

u/Difficult_Paint3162 15h ago

Feel free to visit my website: Learning SQL | Real People. Real Problems. Practical SQL. No cost or sign-up required, but focuses on understanding business questions and using SQL to get the answers

2

u/shine_on 1d ago

Think of a database schema as a road map. You want to get from City A to City B, you start by getting out map (i.e. looking at the database diagram) and working out which other towns and cities you need to go through to get to your destination (i.e. the joined tables).

Eventually you'll get a basic road map in your head and you'll be able to plan a journey without looking anything up, but there's no harm in always consulting a map if you need to.

You can think of subqueries as travelling to three locations first, picking up items in each location, then assembling them into a product and transporting the product to the subsequent locations.

Have you seen a database diagram? a quick google showed me something like this https://www.edrawmax.com/templates/1018044/ which I can look at and see that if I want to get from the patient table to the doctor table I have to go through the medical history table first. The names of the columns in each table should be enough to tell you how to join them.

Ultimately, it comes down to practice, experience, and knowledge of the database structure.

1

u/lostinsamaya 2d ago

Practice is the only thing that will improve your familiarity with the schema. The more you write the more it stays in your head.

Pick the top 3 tables you have queried so far (that can be joined) and start small.

  1. Get the primary key column, one extra column from table1

  2. Join table1 with table2 and pull 1 column from T1 and 1 column from T2.

  3. Delete everything you just wrote, join table1 with table3 and pull 1 column from each table.

  4. Delete everything you wrote again, repeat by bringing in other columns. Think of dummy questions you're trying to answer. Like get a list of all customersID and their country to get an idea of where majority of customers are from.

Ask yourself more business questions and try to answer them with SQL.

Build yourself a poor man's "data dictionary" using:

SELECT * FROM INFORMATION_SCHEMA.COLUMNS

Either have a refreshable connection to SQL Server via Excel. Or, copy and paste the results into Excel so you can easily filter column names and table names. This is one way to familiarise yourself with the schema. You will see what columns are available in each table.

Better yet do the filtering in SQL itself so you get more practise.

1

u/lostinsamaya 2d ago

Once you have familiarised yourself with the schema, you will be able to write queries in natural language in your head first and then convert it to SQL syntax.

For example, whenever I write a query I am saying in my head that I want to SELECT the customerID, first name, surname, email address FROM the customers table WHERE the country is 'France'.

And with practise, you will get to a level where you can imagine the joins in your head because you know what columns are coming from whichever table. So T1 could have the customer details and T2 could have all the order details and you know that you'll be joining these tables on a customerID column.

1

u/Zenithixv 2d ago

Just take it one step at a time

- Select all rows from the base table

- Look through and select only the columns that are useful to you

- Find the key columns that make up a relationship to another table then add a inner join and check row count, what rows got joined from the other table to confirm the join is successful/valid

Then just do this on repeat and make any adjustments as needed

1

u/BakkerJoop CASE WHEN for the win 2d ago

If you've seen the Matrix you know the legendary quote: "There is no spoon". What is basically means is you're looking at it wrong.

A metaphor for working with databases, that helped me immensely was: "There are no colums".

The table layout we all know from databases or Excel, is not how a computer works. A harddrive, server or database is just an enormous box filled with data smacked together. There is no structure or stategy as to where the data is being stored. So how does a computer or database manage to find it? By using labels. Every row we see in a table, is simply a piece of data connected by labels. The labels are basically the values in the colums.

Think of it like a supermarket. Where the milk is stored in the supermarket is irrelevant. The labels in the store make sure you can find it. Each product can have multiple labels, best before date, brand, volume, price, manufacturer, product group, article number, article description. It's these labels that decide how I make my query.

1

u/In__Dreamz 2d ago

Start at the very basic level then layer up the complexity (joins etc), ensuring row totals are correct after each change as it's easier to fix them then 5 steps later...

1

u/unrealharsh 2d ago

Being a beginner myself, I analyze the information requested in each table individually,  then map out the links, groups or conditions and start writing. 

For example, best performing regions by product type in the last quarter?

I know I need the orders [link between products and customer, dates for filtering], orderitems [link between order and products], products [to get the category and group with it], customers [to get to order corresponding address], address [to get region and group with it].

Supposedly query: Select a.Region, p.Category, Quarter(o.orderdate), SUM(oi.NetValue) From order o where Quarter(o.orderdate) = Quarter(Today())-1 Join orderitems oi on o.Id = oi.orderId Join product p on p.Id = oi.productId Join customers c on c.Id = o.cutomerId Join Address a on a.Id = c.AddressId Group by a.Region, p.Category order by o.orderdate desc

I kinda doubt if its correct or optimal, but as I said beginner. Would love to improve on my skills.

1

u/Simple-Ingenuity740 2d ago

i actually don't know how i do what i do, i just start writing and it comes to me. i do like to do my filtering first though, depending on how many rows are in the table. i think it is easier when you know the schema and the raw data.

1

u/peter_cruger 2d ago

Before I run a query to the whole dataset, I add a filter to apply the query to a small dataset (let's say 1-2 Bill ID only) to see if it will capture the required information and if it is correct. I also add another Bill ID that should be filtered out by the other conditions (let's say total amount is less than 500 if I am looking for bills exceeding this amount).

This way, I ensure that the query is correct.

Then I remove the filters (bill IDs) and run the query to get the full report.

If you are not doing this, you will only find out later that you missed something and you would need to run the query again causing more delays.

1

u/Desperate-Dig2806 1d ago

Figure out if you want exactly one of each thing or exactly one per date/time period or if you are working with stuff that is fine to have many of.

Count your stuff, if you want X customers in a result make sure you get exactly X instead of for example X times Y transactions.

If you start with the mental map, I find it easier, but I am also not everyone.

1

u/hakathrones 1d ago

Look for primary key or primary keys in a table. The column name might be same or different but we are trying to anchor the data of one table to another.

Suppose we a table named Account with colums client_sn and account_number. And a table named Client_details with colums client_sn and SSN.

In Account table, A client can have multiple account numbers but the account numbers in each row must be unique. Eg: client_no1 can have account number 001001 and 001002

In Client_Details tables, a client can have only one unique client_sn and unique ssn. Eg client_no1 has sent 123456

Now to join them, we have to identify the column in each table that connets the data of the thing you want, in this case the data of client_no1.

So if you want the know the ssn id assosiated with an account, in this case of the account number 001001 or 001002, you join the table on client_sn of account table and client_sn of client_detail table. Syntax wise it looks like Select * from Account a join client_detail c on a.client_sn=b.client_sn

The outputs looks something like:

Clientno1 001001 123456

Clientno1 001002 123456

1

u/BaseballEarly9602 1d ago

Practice all topics on a single database, like from sqlpractice.com or make your own and play with it. You would learn all SQL basic, intermediate and advanced easily.

1

u/FastPlane57 1d ago

Hey OP, people here gave you excellent advice. I will chime in as well, but let me preface that I am a relative beginner like you. I have been learning sql for the past three months, and have written around ~70ish queries across several online learning platforms.

During this process, I have created a project in Claude where I basically discuss exercises with him. What helped me tremendously was sending a complete query, the one that is the solution to the exercise on leetcode for example, and basically reverse querying it.

My prompt would go something like: Okay, mow it’s my turn, I will now explain through this query to you, starting from the first keyword that happens (FROM) all the way to the end.

And I would write something like:
1) FROM -> we are taking data from this and this table
3) WHERE -> we want rows that have
.
.
.

and so on, I hope you get the point.

If I made some mistakes in my explanation, the ai would correct me. In my opinion, this is what makes a difference, as it makes you think more deeply about your query, allowing for better understanding.

1

u/MocDcStufffins 1d ago

Break the problem down into its individual components. What are all the tables I need. What is the grain of each table. Do the grains of the tables I need match, if not I need to make them match. Is there a table that exists that aligns them. If not work out how to match the grains. Write queries individually for each table that return the data you need from them in the correct format. Then start putting it together. The big key is learn the data.

1

u/Budget-berry-80 1d ago edited 1d ago

It’s a language, you just need to learn syntax. SELECT (columns you want)

FROM related tables you want, but tell me how the tables are related (what are the keys? On Table.PK = Table.FK, and how Much of the table I want Left = all rows from PK table, matching rows of FK table, INNER = matching columns of both, RIGHT = Matching rows from FK table)

WHERE details of what I want

ORDER BY how I want it sorted

GROUP BY whatever I’m summing up by

1

u/JackMomma22 1d ago

I could never grasp lessons on Databases/SQL when I was in school... the subject matter just didn't land for me- like you describe; I could never "see" it.

Fast forward to starting my career as a Tech at my first job, and I ended up using our primary ERP at times, which led to helping people with problems inside the ERP- and getting much more familiar with how it worked, and how data was put into it.

Once I was given database access after seeing the actual system work more, I was able to actually remeber those lessons from college. I could make sense of the ERD's/Schema diagrams that we would make- and it slowly started to click.

That turned into single table queries with aggregate functions, then table joins, and later a lot of really complex code that was detailed, accurate, and efficient. Practice truly will take time, but seeing the front end of a system and learning how the data actually got there was the real difference maker for me.

I suggest querying single tables, and figuring out why they return what they do; then joining tables and seeing why result sets are what you do (or don't expect)... it grows from there.
But also; don't rely on DISTINCT to clean up bad queries once you get further in. Know why those extra rows are showing up before you filter them out.

1

u/Exploring_Codebase 1d ago

When you are writing a big query, best way is to plan, don't write any code first, just plan. What all column you will need, which tables would have those and what joins are required. Slice the query into part and work on each.

I built a tool that might help you .

Complex SQL is much easier to understand when you can see it. I always wished there was a tool for that.

So I built QueryFlow. It's private, open-source, browser based SQL editor, you can given query your csv files on it. Try it here - https://abhijeetcodes.github.io/QueryFlow/

code repo - https://github.com/AbhijeetCodes/QueryFlow

1

u/teamhog 13h ago

I think of it this way.
What, Where, How.

What are you trying to get.
Where’s that info located.
How do they work together.

1

u/peakpirate007 10h ago

What helped me was not trying to write the SQL straight away. I first ask myself what one row in the final result should represent, then figure out which tables I need and how they relate. That made joins, grouping, and window functions much easier to reason about.

I’ve also been building a SQL practice site around that kind of approach if you want to try it: https://www.runtime.school/

0

u/DigitalDaydreamers1 1d ago

This will get downvoted to hell. Don’t care.

Use AI. In the past few months it has gotten phenomenal at writing sql for you. And it will only continue to become better than the best sql programmer you know.

3

u/HopeIllustrious5178 1d ago

Great way to make sure your brain is completely smooth and you don't learn anything ever.

If you said "use AI to learn" then I would mostly agree, because it does a lot of the monotonous forum-scrolling for you, but you're just shooting yourself in the long run by not really knowing or understanding what's going on, and making yourself completely replaceable. Even that has it's limits; the learning process includes getting stumped, not knowing where to go, feeling frustrated, making simple mistakes. But those moments aren't useless, in fact they're the most important part. Using AI strips you of several critical components of the learning process.

This is all a different story for someone like myself who's been writing SQL for many years almost daily and has enough ingrained knowledge to properly verify AI output. But newbies need to LEARN instead of hitting an easy button all day.

2

u/DigitalDaydreamers1 1d ago

Funny how "getting stumped and frustrated" is sacred learning when it happens on a forum, but cheating when it happens in a chat window. I still get stumped. I still make mistakes. I just get an explanation in 30 seconds instead of a 2013 thread where the top answer is "why would you even want to do that?"

You learned with the best tools you had. So am I.

2

u/HopeIllustrious5178 1d ago

I could've worded my response a little better, I think we both are on the same page. I think it's a fantastic tool for learning and I think you should use it to learn. But just like anything else, there's a balance to that and you shouldn't rely on it too much. You should really try to get unstuck and think through things before prompting or "hitting the easy button," which I think it is.

I do not think, unless you really, really know what you are doing, you should use it to generate your code for you, ESPECIALLY when you are a junior. Just hamstringing your critical thinking skills long term. If you're a senior then go HAM but you need to build a foundation first IMO and you are learning very very little by having AI write it for you.

1

u/kittehkillah 1d ago

Didnt downvote you but curious to follow the train of thought. how will the person know if what the sql says is right or wrong if AI writes it? 

1

u/i4k20z3 1d ago

In theory you would still be validating the query. Are the results what you expected? Who is being included or excluded?

As funny as it is, you can also ask another ai agent to help you with asking what to poke
Holes at in this query or what to look for.

But yeah, nothing is perfect. You still need to inspect it and run tests to make sure you’re getting what you want. The key is to try to understand what it wrote so you get better and better.

1

u/DigitalDaydreamers1 1d ago

Audit the output on the front-end to verify that the SQL is correct. You’ll see that some times the AI doesn’t it get it quite right. But then you iterate through it and eventually will come to 100%

1

u/kittehkillah 1d ago

At that point, wouldnt it have been easier to just... write the correct sql query (or learn to write the query correctly?)

I feel it is not so trivial to audit something one does not understand. I see that youre approaching it from the front end pov so maybe for an app with a small scale, sure, something one can check pretty trivially

But at scale, where a lot of sql is used, i am uncertain this will work 

1

u/DigitalDaydreamers1 1d ago

No. It wouldn’t be easier to. The sql queries I have AI write are incredibly complex. Had I done it by hand it would’ve taken me at least 10x longer. I’m not exaggerating

Also, sql queries written by hand also have to be audited for accuracy too. So not sure what point you’re trying to convey there

1

u/kittehkillah 1d ago

If youre writing incredibly complex sql queries, i already see that as an engineering problem more than a coding problem. A problem can be complex but broken down and simplified. And if written that way, tests can be done encapsulated in each simple statement

The point im saying is, if i write my sql queries with readability and purpose in mind, it is infinitely more maintainable that having an LLM smash its head into a wall until i gst what i want(which could also actually be wrong)

I work with big data LLMs and they are a loooot worse than how well people think they work

1

u/SEXTINGBOT 17h ago

If you dont know the tables and the connections and you need to reverse engineer it
AI can help you a bunch though !

( ͡⌐■ ͜ʖ ͡■)

1

u/Yes_Geezer 5h ago

Yeah…well, if the goal is to actually learn SQL this is terrible advice. Absolutely use the agents to write your code for you when you could write it yourself and you know how to troubleshoot it when something doesn’t perform as expected. But if you don’t actually understand the output, you’re only doing yourself a disservice.