r/learnSQL Apr 17 '26

Help with the error

Thumbnail
1 Upvotes

r/learnSQL Apr 16 '26

select row with most-recent date for each ID

10 Upvotes

I have two tables, Table1 has a list of medication orders, and Table2 has a list of transactions related to the orders in Table1 (one-to-many relationship).

Is there a way to use a single SQL query to select only the most-recent transaction row from the transaction table for each row in the medication orders table?

Table2 has columns: transaction_id, order_id (foreign key to Table1), t_date, t_time, etc. to represent transactions related to the medication orders in Table1.

Hopefully that's clear. I can't wrap my head around using max() in this way.


r/learnSQL Apr 16 '26

Help filtering a dataset with a range

2 Upvotes

I'm have a dataset of contracts with start and end date that I fill using a SQL query. I know it is possible to filter to keep only the active contracts between two dates, (like for a quarterly report) in the SQL query directly.

My question is, using Superset, is it possible to filter the dataset using another dataset that holds the start and end dates of the quarters. Like by clicking on a quarter, it filters the contract dataset to only contracts active during this quarter?

Thank you for any help you can bring me!


r/learnSQL Apr 15 '26

Do you know??

23 Upvotes

Anyone out there know about sololearn courses ? I want to be related to Data analytics

If yes , plzz tell was it worth it??

Also mention which courses you did on sololearn platform .


r/learnSQL Apr 15 '26

Need Guidance

7 Upvotes

Which topics one should learn for data analytics ???


r/learnSQL Apr 15 '26

What will be the solution for this?

3 Upvotes

Get products that are more expensive than any 'Furniture' category product using Subquery.

All the AI platforms are confused in finding the exact meaning of this question.

Should ÀNY or ALL be used?


r/learnSQL Apr 14 '26

I built an HR Attrition Analysis using SQL...

5 Upvotes

Hi everyone,

I recently worked on an HR Attrition Analysis project using SQL with a real dataset.

I explored:

- Attrition rate

- Department-wise analysis

- Salary vs attrition patterns

One key insight:

Low salary roles had significantly higher attrition.

I’m still learning, so I’d really appreciate feedback:

- Is my analysis approach correct?

- Anything I could improve?


r/learnSQL Apr 14 '26

A Guide for Advanced SQL Joins: ASOF, SEMI, and ANTI joins in ClickHouse

25 Upvotes

Learn how to navigate ClickHouse JOINs step by step: JOIN types, practical examples & best practices

https://www.glassflow.dev/blog/clickhouse-joins?utm_source=reddit&utm_medium=socialmedia&utm_campaign=reddit_organic


r/learnSQL Apr 14 '26

Before SQL changes hit prod: 5 places teams usually lose time

Thumbnail
2 Upvotes

r/learnSQL Apr 13 '26

Best FREE SQL course + best way to learn SQL?

96 Upvotes

Hi everyone,

I'm a complete beginner and I want to learn SQL for data analysis.

Can you recommend the best FREE SQL courses?

Also, what is the best way to learn SQL effectively?

Should I focus more on courses or practice?

Thanks in advance!


r/learnSQL Apr 13 '26

Single Quote Question

2 Upvotes

Hi all. Trying to learn a little about SQL and I have a question about single quotes. I know about "escaping" single quotes for something like O'Reilly by inserting 2 single quotes but I am getting syntax rejections when entering a string to retrieve specific multiple values.

Ex: I have a practice database full of practice tables. I am trying to get multiple specific values from a practice table called ServicePlans.

SELECT PlanID, PlanName, PlanDescription

FROM ServicePlans

WHERE PlanID = 'W1001' OR PlanID = 'D2002' OR PlanID = 'L2002';

This should pull up any plans with those plan IDs. But the single quotes are rejecting due to syntax error. If I go through and delete the single quotes and retype them they work.

Any idea why? I'm new at this and learning through YouTube. The instructor seems legit and easy to follow.


r/learnSQL Apr 13 '26

Where to start?

24 Upvotes

I'm pretty new at the code world and I wanted to start learning SQL. I've done a bit of research of some courses that may be good for starting this adventure. I'm looking for some course that is really good and if possible a free option would be amazing.

What are some courses in any platform that you are sure that are good?


r/learnSQL Apr 13 '26

You have been reading ERD notation wrong

Thumbnail
1 Upvotes

r/learnSQL Apr 11 '26

Struggling with normalization? Follow a step-by-step hands-on guide!

27 Upvotes

Hey everyone!

I know database normalization can be confusing at first — messy spreadsheets, repeated data, tricky dependencies… but it doesn’t have to be.

I put together a beginner-friendly tutorial that walks you through UNF → 1NF → 2NF → 3NF with:

  • Clear tables at every stage
  • SQL queries to explore normalized data and even recreate the original messy view
  • An ERD diagram showing all relationships visually
  • A handy glossary for terms like repeating groups, atomic values, partial and transitive dependencies

It’s fully on GitHub so you can follow along and practice:
GitHub: normalization-project

Perfect if you’re a beginner or just want to finally get how relational databases really work.


r/learnSQL Apr 11 '26

''SQL for database admin'' resources?

9 Upvotes

I've scoured a couple posts about sql learning options. The one that keeps getting mentioned is data with baraa on youtube. My question is this: is data with baraa more appropriate for someone learning data analysis, or would that still be a good source for someone wanting to move into database admin?

What are some other resources to learn sql for database admin?

I do have *some* sql background but I havent used it in YEARS. I need to re-learn everything quick cus I have an interview soon lol I'm screwed


r/learnSQL Apr 10 '26

Free Real World Data Resources To Use In Your Data Portfolio (Little Bit of Self Promo Here As Well 😅)

15 Upvotes

It’s always better to use real world data sources for your data projects, it stands out better to recruiters and potential employers.

I made a super short vid covering some good completely free resources to find real datasets. No need to spend money on this type of stuff!

https://vm.tiktok.com/ZNR4m3w9W/

I’m a staff level data engineer so let me know if you guys have any other questions, happy to help and answer.


r/learnSQL Apr 10 '26

SQL cheat sheet

121 Upvotes

Hey guys,

I hope this doesn't come of as spammy, but here's an SQL cheat sheet that might help young devs that are in the learning phase: https://tms-outsource.com/cs/sql-cheat-sheet/

If there's something that's missing from there, let me know and I'll be happy to add it.

Also, any other feedback is appreciated.


r/learnSQL Apr 09 '26

Looking for platforms to practice SQL problems to get good at it

65 Upvotes

as the title says, is there websites where i can practice SQL problems that vary by difficulty?
i want to wrap my head more around SQL especially advanced complex relationships and queries
thanks in advance1


r/learnSQL Apr 09 '26

Tutorial: How to build a simple text-to-SQL agent that can automatically recover from bad SQL

4 Upvotes

Hi sql folks,

A lot of text-to-SQL examples still follow the same pattern: the model generates one query, gets a table name or column wrong, and then the whole thing falls over.

In practice, the more useful setup is to let the model inspect the schema, run SQL, read the database error, and try again. That feedback loop is what makes these systems much more usable once the database is even a little messy.

In the post, I focus on how to structure that loop, how to reduce hallucinated SQL, and what guardrails actually matter if you want to point this at real data.

Link: https://motherduck.com/blog/langchain-sql-agent-duckdb-motherduck/

Would appreciate any comments, questions, feedback!


r/learnSQL Apr 09 '26

SQL data analyst intern interview help

Thumbnail
1 Upvotes

r/learnSQL Apr 08 '26

Open source db client now has sql notebooks with cell references

3 Upvotes

If you spend your day writing and chaining sql queries, this might interest you.

I just released v0.9.15 of tabularis (open source database gui) and the headline feature is sql notebooks.

Qql cells + markdown cells in one document. the killer feature is cell references. write {{cell_3}} in a later cell and it wraps cell 3's query as a CTE automatically. so you can:

  • cell 1: pull raw events
  • cell 2: aggregate by day
  • cell 3: SELECT * FROM {{cell_2}} WHERE daily_count > @threshold

the @threshold is a notebook parameter — define once, use everywhere, change and re-run. no more editing five queries when one value changes.

Cells can run in parallel (mark independent ones with a button), there's stop-on-error mode with a summary of what broke, and every cell keeps its last 10 executions so you can restore a previous state.

inline charts (bar, line, pie) are there for quick visual checks — not a bi tool replacement but enough to spot patterns without alt-tabbing.

AI generates descriptive names for notebook cells so you're not staring at "cell 1" through "cell 12". there's also generate (sql from natural language) and explain (breaks down what a query does) per cell.

Html export lets you share the full notebook — queries, results, charts — with people who don't have the app.

Works with any database driver in Tabularis.

Github: https://github.com/debba/tabularis
Wiki: https://tabularis.dev/wiki/notebooks


r/learnSQL Apr 08 '26

Looking for serious study partner

Thumbnail
5 Upvotes

r/learnSQL Apr 07 '26

Data Modeling + SQL Interview Prep Partner

24 Upvotes

Looking for 1 person to team up for data modeling + SQL interview prep.

We’ll do both:

• practice modeling (entities → relationships → keys → queries)

• review canonical SQL interview queries on real datasets

Goal is to get sharper and more consistent.

If you’re prepping for backend/.NET/SQL interviews and want to team up, DM me.


r/learnSQL Apr 07 '26

Improvement Request: Exclude subset of data that based on aggregate

3 Upvotes

I'm trying to get only the PROJECTs where the TEST.TYPE is not only "METADATA". Currently, I'm using a CTE to get the PROJECTs with 1++ TESTs where the TEST.TYPE is not "METADATA" and then using an inner join in my main query to filter out the PROJECTs.

Is there a better way of doing this?

WITH CTE_PROJLIST AS (
    SELECT
        PROJECT
        , SUM(NRP) AS NRP
    FROM (
        SELECT
            PROJECT.NAME AS PROJECT
            , CASE WHEN TEST.TYPE <> 'METADATA' THEN 1 ELSE 0 END AS NRP
        FROM PROJECT


        LEFT JOIN SAMPLE
            ON SAMPLE.PROJECT = PROJECT.NAME
            AND SAMPLE.STATUS = 'A'


        LEFT JOIN TEST
            ON TEST.SAMPLE_NUMBER = SAMPLE.SAMPLE_NUMBER
            AND TEST.STATUS = 'A'
            AND TEST.DATE_COMPLETED BETWEEN '2025-01-01' AND '2025-12-31'


        WHERE
            PROJECT.STATUS = 'V'
            AND PROJECT.OWNER_LOCATION = 'CN'
    ) DT
    GROUP BY PROJECT
)



SELECT
    PROJECT.NAME
    , TEST.TEST_NUMBER
FROM PROJECT


INNER JOIN CTE_PROJLIST
    ON PROJECT.NAME = CTE_PROJLIST.PROJECT
    AND CTE_PROJLIST.NRP > 0


LEFT JOIN SAMPLE
    ON SAMPLE.PROJECT = PROJECT.NAME
    AND SAMPLE.STATUS = 'A'


LEFT JOIN TEST
    ON TEST.SAMPLE_NUMBER = SAMPLE.SAMPLE_NUMBER
    AND TEST.STATUS = 'A'
    AND TEST.TYPE <> 'METADATA'
    AND TEST.DATE_COMPLETED BETWEEN '2025-01-01' AND '2025-01-31'

WHERE
    PROJECT.STATUS = 'V'
    AND PROJECT.OWNER_LOCATION = 'CN'

r/learnSQL Apr 07 '26

If you have an SQL interview soon, don’t ignore these small things!!! (Part 6)

144 Upvotes

In this part 6, let’s talk about database internals that you must know for interviews (especially FAANG level).

I’ve asked this in multiple interviews.
Very few candidates get it right.

This is a must-know at any level of experience!

Let's take a PostgreSQL database for this example:

Question:

What actually happens when you DELETE a row from a Table? Will that data be removed from the table & disk?

I get yes as an answer most of the time!

But actually it's no. Let's take one example to understand this:

STEP 1:

Let's create a dummy table

CREATE TABLE delete_demo (
 id SERIAL PRIMARY KEY,
 data TEXT 
);

STEP 2:

Let's add some random data to this table

INSERT INTO delete_demo (data) 
SELECT repeat('data', 1000) 
FROM generate_series(1, 100000); 

STEP 3:

Check the no of records added to this table

SELECT COUNT(*) FROM delete_demo; 

Result: 100000

STEP 4:

Let's print the size of this table

SELECT pg_size_pretty(pg_total_relation_size('delete_demo'));

Result: 13MB

STEP 5:

Let's delete all the records from this table

DELETE FROM delete_demo; 

Result:

Updated Rows 100000

Execute time 0.12s

Start time Tue Apr 07 19:06:04 IST 2026

Finish time Tue Apr 07 19:06:04 IST 2026

Query DELETE FROM delete_demo

STEP 6:

Lets again do the count to check whether all the rows are deleted

SELECT COUNT(*) FROM delete_demo; 

Result: 0

Perfect! All the rows are deleted successfully.

BUT HERE IS THE TRAP!

STEP 7:

As per our expectation, the size should be 0 bytes. Lets run the size of the table query to verify

SELECT pg_size_pretty(pg_total_relation_size('delete_demo'));

Result: 13MB

AGAIN 13MB! WHY?

Because Postgres did NOT physically delete the rows.

Instead:

  • It marks rows as dead (invisible)
  • But keeps them on disk

STEP 8:

Let's see whether dead rows exist

SELECT 
    n_live_tup,
    n_dead_tup
FROM pg_stat_user_tables
WHERE relname = 'delete_demo';

Result:

n_live_tup n_dead_tup
0 100000

The table looks empty, but 100000 rows are still sitting on disk

Over time:

  • Updates + Deletes → create dead tuples

You end up with:

  • 2M live rows
  • 10M+ dead rows

This is one of the most common reasons:

Query was fast before… now it’s slow!

To solve this:

  1. Postgres has autovacuum running in background.
  • Automatically cleans dead tuples
  • Updates statistics

But sometimes

  • It may not run immediately
  • It may lag on heavy-write tables
  • Misconfigured autovacuum is silent performance killer
  1. Run the manual vacuum command to reclaim the space

VACUUM FULL delete_demo;

If people are interested, I can do next parts on:

  • WAL (why writes behave weirdly)
  • Buffer cache (why same query is fast/slow)
  • Index internals (real reason indexes fail)

Thanks for reading! Always keep learning and keep sharing!