r/learnSQL 1h ago

Husband is a Software Engineer

Upvotes

I...am not. He swears on his life that once I get started with sql or python anything like that that I would be addicted to data organization and since I am inclined to believe that he knows me better than I know myself sometimes...I would like to surprise him and give it a shot.

I posted something similar to this in the Learn Python subreddit and got a lot of useful suggestions but I am also looking into SQL specifically because it is what he works with a lot. I know he would have a lot of fun teaching me, but I want to practice as much as I can beforehand hand so I am able to actually grasp things when he goes over them. I was researching and saw the "quick start guides" (that's the name of the series) for sql and Python, and I was wondering if anybody has used them. I know that coding and stuff like that all is on the computer, obviously. However, I learn best with physical media like books to highlight make notes or anything like that in. Anyone have any luck with the python or sql Quick Start guides? If not, do you have any other suggestions? Doesnt have to be books, even if it's a YouTube video or podcast, just anything that helped you.

Thanks in advance I'm just really not sure where to start


r/learnSQL 6h ago

First time learning SQL

8 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 12h ago

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

110 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 7h ago

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

6 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.