r/SQL • • 23h ago

Discussion Does this table meet the normalization requirements (1NF, 2NF, and 3NF)?

Post image
0 Upvotes

I’m working on a database project for a metro system, and I’m not sure if my Train table satisfies 3NF.
I think Capacity depends on TrainModel rather than TrainID. If I enter different capacity values for trains of the same model, would that solve the problem, or would it still violate 3NF?
Should I remove Capacity from the table?


r/SQL • • 17h ago

Discussion LAST_VALUE kept giving me the current row instead of the last one. the default window frame was the reason

4 Upvotes

wanted the latest order amount next to every order for each customer, so i wrote this:

SELECT customer_id, order_date, amount,
LAST_VALUE(amount) OVER (
PARTITION BY customer_id ORDER BY order_date
) AS last_amt
FROM orders;

and got this:

customer_id | order_date | amount | last_amt
1 | 2025-01-05 | 40 | 40
1 | 2025-02-11 | 25 | 25
1 | 2025-03-02 | 60 | 60
2 | 2025-01-20 | 90 | 90
2 | 2025-02-28 | 15 | 15

last_amt is just amount again. FIRST_VALUE on the same window worked fine, which made it more confusing

the reason: once you put ORDER BY inside OVER, the default frame is `RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW`. so the "last" row of the frame is always the current row (or its last tie). FIRST_VALUE doesnt care because the start of the frame is still the first row

fix is to spell out the frame:

LAST_VALUE(amount) OVER (
PARTITION BY customer_id ORDER BY order_date
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS last_amt

now customer 1 gets 60 on every row and customer 2 gets 15

or skip LAST_VALUE and flip the sort: `FIRST_VALUE(amount) OVER (PARTITION BY customer_id ORDER BY order_date DESC)` gives the same result

same default frame in postgres, sql server, mysql 8 and sqlite (tested on sqlite). also the reason a running SUM can act weird when two rows have the same date, since RANGE treats ties as one group

anyone actually use LAST_VALUE with the full frame or do you all just do the FIRST_VALUE DESC trick?


r/SQL • • 13h ago

SQL Server SQL Server Interview Questions and Answers Part - 74

Thumbnail
reddit.com
1 Upvotes

r/SQL • • 1h ago

MySQL Is SQL still the first thing to learn for analytics in 2027?

1 Upvotes

Yes, SQL is still a practical first skill.

Usage data supports that choice. SQL passed HTML/CSS to become the second most-used language in the 2026 Stack Overflow survey, at 58% (Stack Overflow 2026).

The exam requirements also support it. DP-600 lists SQL, KQL and DAX for querying and gives 45 to 50% to preparing data (DP-600 study guide).

AI tools write SQL, but 48% of respondents say they trust AI output when they can easily verify it (survey data). Verifying a query takes SQL.

Have AI tools changed how much SQL you write by hand?


r/SQL • • 14h ago

MySQL MySQL InnoDB – Index Gap Deadlock with Concurrent INSERT … SELECT

Thumbnail reddit.com
1 Upvotes

r/SQL • • 6h ago

Discussion How to progress with SQL?

13 Upvotes

I’ve been writing SQL daily at work for about 2–3 years (mostly Redshift, risk/analytics). Like a lot of people, I learned it on the job: figure out what the business needs, google my way through it, use Claude, repeat. It’s gotten me pretty far, but lately I’ve felt like I’ve plateaued.

The problem is I don’t know what I don’t know. My queries work, but I have no real benchmark for whether they’re good, and nobody’s ever really challenged how I approach problems or poked hole in the logic. I’ve done some self-paced stuff (Udemy, practice problems), and it’s fine for learning syntax, but it doesn’t tell me where my blind spots are.

So I’ve been thinking about finding an actual person to learn from, either a tutor or an instructor-led course where someone reviews your work and pushes back. I’m in the Burlington/Hamilton area, so in-person is an option, but live online works too.

For anyone who’s been in a similar spot: Did working with a tutor or instructor actually make a difference, or did you find another way to level up? If it worked, how did you find the right person or course for someone who’s past the beginner stage?

Would really appreciate hearing what worked (or didn’t) for you and how you got over this feeling of hitting a wall.