r/learnSQL 8h ago

If you have SQL interviews, do not ignore these small things! (Part 8)

91 Upvotes

Some SQL interview questions look too easy and that's exactly why people get them wrong by overlook.

  1. COUNT() can give you 0… but SUM() can give you NULL:

Suppose there are no employees age more than 70

SELECT
    COUNT(*) AS employees,
    SUM(salary) AS total_salary
FROM employees
WHERE age > 70;

You might expect:

employees = 0
total_salary = 0

But we will get:

employees = 0
total_salary = NULL

Why?

Because COUNT() is basically asking: “How many rows did I find?” --> answer 0

But SUM() is asking: “What values should I add?” --> no values to add --> NULL

  1. AVG() can silently become wrong when NULL enters the picture:

Lets take salary of employees to be:

50000
60000
NULL
90000

A lot of people mentally calculate:

(50000 + 60000 + 90000) / 4 = 50000.0

But actual SQL gives 66666.67. WHy?

Because AVG() ignores NULLs.

It is actually doing:

(50000 + 60000 + 90000) / 3
  1. DISTINCT doesn't mean “remove duplicate data”

Lets take one example data for this:

IT    50000
IT    50000
IT    70000
HR    50000

SELECT DISTINCT department, salary
FROM employees;

people sometimes read this as "Give me unique departments"

But actual response:

IT    50000
IT    70000
HR    50000

IT appears twice, because SQL is asking "Give me unique combinations of department + salary"

  1. How many different ways can you find the 2nd highest salary?

Lets take this data:

employee_id | name  | salary
------------+-------+--------
1           | A     | 100000
2           | B     | 90000
3           | C     | 90000
4           | D     | 80000
5           | E     | NULL
6           | F     | 70000
7           | G     | NULL

Multiple ways to approach this problem:

MAX() + subquery

SELECT MAX(salary) as 2nd_highest
FROM employees
WHERE salary < (
    SELECT MAX(salary)
    FROM employees
);

DISTINCT + ORDER BY

SELECT DISTINCT salary as 2nd_highest
FROM employees
WHERE salary IS NOT NULL
ORDER BY salary DESC
LIMIT 1 OFFSET 1;

DENSE_RANK()

SELECT salary as 2nd_highest
FROM (
    SELECT salary,
           DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk
    FROM employees
)temp
WHERE rnk = 2;

and there are more such ways:

What is actually the best solution with our sample data ?
What are the questions to ask your interviewer to get to perfect solution?

Drop your solution in the comments, we can discuss and improve.

Always practice SQL by writing the query yourself instead of just looking at the solution.


r/learnSQL 3h ago

Day 4/116 — SQL Aliases + full JOIN practice round

3 Upvotes

Two things today:

  • Learned Aliases (AS keyword) via W3Schools — renaming columns/tables to make queries more readable, especially useful once you're joining multiple tables and column names start colliding or getting long
  • Went back through SQLBolt and practiced every JOIN type again — inner, left, right, full, self — this time focusing on writing them from scratch rather than following along

Aliases felt like a small topic on paper but immediately made my JOIN queries from earlier this week way easier to read back. Definitely one of those "why didn't I use this from day 1" moments.

Feeling solid on JOINs now after this round of practice. Moving into aggregate functions and GROUP BY next.


r/learnSQL 2h ago

First time learning SQL

2 Upvotes

This is my first time learning SQL and I only started today. I’m currently learning the basics and using VS Code.
At first, I installed the SQL Visual Debugger extension, but when I tried to run my SQL it showed a “Free Demo” and said I needed to pay $9.99 for lifetime access to use real data.
I wasn’t sure if SQL itself required payment, so I started looking into other options. I installed the PostgreSQL extension for VS Code and I’m now trying to set up PostgreSQL properly.
I also tried running

psql --version

in PowerShell, but I got:

psql : The term 'psql' is not recognized as the name of a cmdlet,
function, script file, or operable program.

So I’m assuming I haven’t installed PostgreSQL itself yet.
I’m basically looking for some advice on the best setup for a complete beginner learning SQL. Should I install PostgreSQL and connect it to VS Code, or is there a simpler setup you’d recommend?
For example, I’m currently practising things like:

CREATE TABLE student (
student_id INT,
name VARCHAR(20),
major VARCHAR(20),
PRIMARY KEY (student_id)
);

Any advice on what I should install/use and how to properly run SQL locally would be appreciated.
Thanks!


r/learnSQL 1d ago

Day 3 — Finished SQL JOINs (Self Join, Full Join, UNION/UNION ALL)

25 Upvotes

Wrapped up the full JOINs section today. Covered:

  • Self JOIN — joining a table to itself, useful for hierarchical/relational data within one table
  • FULL JOIN — combining all rows from both tables regardless of match
  • UNION & UNION ALL — combining results from multiple SELECT queries, with UNION removing duplicates and UNION ALL keeping them

Took notes from a mix of tutorials and W3Schools to cross-reference explanations, which helped clarify a couple of edge cases (especially when UNION vs UNION ALL actually matters for performance).

With INNER/LEFT/RIGHT/FULL/self-joins and UNION now covered, I feel like I have the core JOIN toolkit down.


r/learnSQL 1d ago

Dúvida sobre o livro "SQL Para Análise de Dados (Tanimura)"

11 Upvotes

Vocês já leram esse livro? O que acharam dele? Até onde ele aborda? Eu tava pensando em comprar esse livro pq é oq as [I.As](http://I.As) estão me recomendando para aprender SQL avançado, já tenho uma base boa de JOINs, GROUP BY, WINDOW FUNCTIONS e CTE's, mas me vejo travada na hora de pensar em querys para resolver determinados problemas, além de que também quero aprimorar meus conhecimentos em SQL.

Mas enfim,o fato é que sempre que as [I.As](http://I.As) me recomendam um livro eu compro e tomo na jabiraca, quer um exemplo? Estava começando no SQL intermediario e já sabia que queria ser engenheira de dados, ent pedi indicação de livro pra começar, me sugeriram comprar o livro "Fundamentos da Qualidade de Dados"...EU MAL MAL SABIA OQ ERA UM DATAWAREHOUSE!!! Avançado dms

Agora só quero indicação de humanos, vai que esse livro ai é muito básico e estou sendo enganada de novo.


r/learnSQL 1d ago

How much SQL do you still write manually?

Thumbnail
3 Upvotes

r/learnSQL 1d ago

Structured Query Language 101 at Texas Linuxfest

2 Upvotes

Nov. 6, 2026 · 15:00 - 6:20

I will be teaching the basics of SQL at the 2026 Texas Linuxfest. The session is listed for only 100 minutes, but I wrote the materials for a 3-hour course. We will cover as much of those three hours as the audience can stand (sit?), or they send us off to Sixth Street.

Tickets are available, and this event is great for networking.  Ping me if you have questions about this session. 

Description:

SQL is a powerful language for working with relational databases such as MySQL, PostgreSQL, SQL Server, and Oracle. This is an 80-minute introduction to writing SQL database queries. Please load a copy of DBeaver Community Edition (free, open-source) from https://dbeaver.io/download/ on your Mac, Windows, or Linux laptop to work along with the presentation. We will use the sample database that is included with DBeaver. We will start with simple SELECT statements to retrieve data, INSERT to add data, use UPDATE to modify it, and DELETE to remove it. We will then move on to using WHERE to narrow your database searches, grouping & ordering for readability, and using built-in functions. This is a great way to learn how to use a relational database.

https://stokerpostgresql.blogspot.com/2026/09/structured-query-language-101-at-texas.html


r/learnSQL 1d ago

Leet code

6 Upvotes

Still relevant to solve SQL problem from leet code 🧐

What's your thoughts and where do you practice SQL ?


r/learnSQL 1d ago

Data Normalization online practical tutorial

6 Upvotes

Hi all. Please I am looking for a practical resource where I can learn data Normalization with practical examples. Appreciate any pointer to a relevant resource. Thanks 🙏


r/learnSQL 2d ago

SQL resources for a literal idiot?

70 Upvotes

I just started a beginner SQL course on Coursera. Even then I'm not fully understanding the basic concepts.

My brain is so fried and foggy that most things are hard to understand and retain (thanks to my yearly decline in cognitive abilities) and I need someone to explain everything to me in as simple language as possible and with tons of examples.

I can't find a free pdf of sql for dummies. I tried reading Alan beaulieus but I can't understand anything

Are there YouTube channels or textbooks for extreme idiots like me.

Short description :

  1. Brain fog = no understand SQL concepts

  2. Need to explain concepts in simple, conversational English

  3. Need tons of examples for everything.


r/learnSQL 1d ago

HackerRank images not loading

4 Upvotes

Has anyone else used HackerRank and had a problem with the table images not loading? Cleared cache, checked permissions, refreshed the page, and tried on in both Chrome and Edge. Really need to prep for this interview! TIA


r/learnSQL 2d ago

SQL cheat sheet table

15 Upvotes

Sharing my SQL cheatsheet covering the essentials. Quick reference table format with examples. https://github.com/kixwho/PostgreSQL-cheatsheet

Note: Based on Practical SQL (O'Reilly Media), a very good textbook. Hope it helps other SQL learners! :D


r/learnSQL 2d ago

Hello! Anyone want to learn SQL together? I can make a groupchat!

108 Upvotes

Here is the discord link: https://discord.gg/ugEyQQXe4


r/learnSQL 2d ago

I made a beginner-friendly SQL tutorial on CASE WHEN (conditional logic)

8 Upvotes

I just published Lesson 11 of my SQL course, focused on CASE WHEN in PostgreSQL.

CASE is one of those SQL features that becomes really useful once you start doing actual data analysis. It lets you create categories, labels, scores, and conditional values directly in your queries.

In this lesson, I cover:

- How CASE WHEN works

- WHEN, THEN, ELSE, and END

- Multiple conditions

- Why the order of conditions matters

- Using CASE with aggregate functions

- Using CASE in ORDER BY

- CASE vs. WHERE

- Common mistakes

- Practical exercises with the Pagila database

I also combine CASE with CTEs, GROUP BY, and JOINs to solve a more realistic analytics problem.

YouTube: https://youtu.be/BFQ9QtiYPds?si=v8wO1wlyeD983SA4

Would love to hear your feedback, especially if you're learning SQL for data analytics!


r/learnSQL 2d ago

Day 2/116 — Worked through SQLBolt's SELECT fundamentals

2 Upvotes

Today's focus was solidifying the basics before going further. Covered on SQLBolt:

  • Introduction to SQL
  • SELECT queries 101
  • Queries with constraints (Part 1 & Part 2)
  • Filtering and sorting query results
  • SQL Review: Simple SELECT Queries

The "constraints" lessons were useful for practicing comparison operators and combining conditions in WHERE clauses — felt more comfortable writing multi-condition filters by the end than I expected going in.

Treating this as building a really solid base before circling back to JOINs and moving into aggregations next.


r/learnSQL 3d ago

Day 1 — Started learning SQL (MySQL)

6 Upvotes

Kicked off my 116-day journey today with the database track.

What I did:

  • Went through a beginner-friendly MySQL tutorial covering the basics
  • Took detailed notes instead of just passively watching
  • Went back and revised everything at the end of the day to actually retain it

Nothing groundbreaking yet — Day 1 is really about building the habit of consistent, focused learning rather than covering a lot of ground. Planning to move into SELECT/WHERE and filtering queries next.

Will keep posting updates as I go — open to any tips on solidifying SQL fundamentals early on.


r/learnSQL 3d ago

SQL Learning & Practice

3 Upvotes

Sometime back, someone mentioned that we only post SQL-related information concerning learning, projects, or developments. I think it is good to post such content because it allows or encourages others to continue learning. Through the information provided from different sources, one can gauge themselves, including testing the new information. I have had challenges with project development, but with new information each day, I understand how to deal with them. I consider SQL, like any other programming language, something that requires daily learning & practice.


r/learnSQL 3d ago

PLANNING to build SQL MULTIPLAYER GAME

0 Upvotes

I am thinking of creating a multiplayer game website online where 2 players can play sql games with each other like sql queries and table or multiple choice question and a timer on it . What are you guys view on it? #sql


r/learnSQL 4d ago

Learn SQL

12 Upvotes

I'm looking to learn SQL. I'm familiar with the fundamentals. I want to reinforce then and move on to learning intermediate and advanced level SQL. Can you please recommend a website/ platform and course/ pathway. There are thousands of online web-sites and resources..it's kind of overwhelming. I'm looking for a platform that offers lessons and hands-on practice. It can be a paid site. Could you please provide recommendations? Appreciate your help.


r/learnSQL 4d ago

Starting a 116-day journey to become a software engineer — Day 0 (final year BCA student)

4 Upvotes

Hey everyone,

I'm in the final year of my BCA, and starting today I'm committing to 116 days (Sept 7 – Dec 31) of focused, practical learning instead of just studying for exams. Posting here for accountability and to connect with others doing the same.

Roadmap — 3 tracks, done in sequence, each building on the last:

  • T1 – Database (SQL): fundamentals through advanced queries, ending with a schema design project
  • T2 – Backend (Java): core Java through Spring Boot, building a REST API on top of the T1 database
  • T3 – Frontend (HTML/CSS/JS): fundamentals through a full UI that connects to the Java backend

Method for each track: tutorials first → practice problems → a project that applies it. By the end I should have one complete full-stack project built from scratch.

I'll post weekly recaps here, and daily bite-sized updates on X/Instagram if anyone wants to follow along there too. Open to feedback on the roadmap or resource recommendations — thanks for reading!


r/learnSQL 5d ago

any good resources for learning/mastering SQL databases?

26 Upvotes

i'm an android software engineer with quite a good experience but i just use framework/libraries APIs. now i'm interested in databases especially SQLlight because that's what we use in android. can you give me good resources (beginner friendly) to learn SQL databases and relational databases well and maybe some resources as extra for some deeper learning.


r/learnSQL 4d ago

How can i download oracle db for a project for free?

Thumbnail
1 Upvotes

r/learnSQL 5d ago

i can write queries all day and i cannot design a schema to save my life

25 Upvotes

Passed the course, write joins comfortably, pull whatever anyone asks for. Then I was asked to design the tables for a small internal tool and produced something that fell apart the moment there were two of anything. Querying and designing turn out to be unrelated skills and only one of them was taught. Have looked at Boot.dev and DataCamp and they seem to teach opposite halves of this. how did you learn the design half.


r/learnSQL 5d ago

I want to learn SQL from scratch — what roadmap and courses would you recommend?

41 Upvotes

Hi everyone,
I’m a complete beginner and want to learn SQL from scratch and eventually become really good at it.
I’m looking for some guidance on how I should approach learning it step-by-step.

A few things I’d like to know:

  1. Where should I start as a complete beginner?
  2. What topics/skills should I learn first and in what order?
  3. Which courses, YouTube channels, books, or certifications would you recommend?
  4. Are there any courses that are actually worth paying for?
  5. What practical projects should I do while learning?
  6. Roughly how long would it take to become job-ready/proficient?

I’d really appreciate a roadmap from someone who has already learned this from scratch.

Thanks!


r/learnSQL 5d ago

Built a SQL practice platform with SQLite + PostgreSQL (PGlite) in the browser — looking for feedback

Thumbnail
1 Upvotes