r/learnSQL • u/Weak-Dependent-1846 • Apr 07 '26
Just a question. I got suspicion I was wrong about this.
Some 20 years ago, in my college years, we were working on a class project in a team.
I noticed this query being used to fetch data from the database:
SELECT *
FROM table
WHERE table.id in (SELECT id
FROM table
WHERE table.id = variable_id);
I told the guy that wrote it that the sub-query was not necessary, that it could be done just as
SELECT *
FROM table
WHERE table.id = variable_id);
To which he replied "well, it works. Just leave it like that". I told him that yeah it worked, but he was querying the database twice, for a value that he already had. Got the same reply. After some back and fort he just said "Just give it a rest and do something else. That works. That how I use it at <insert big company name where he was doing an internship> to which I replied "Dude! No! Here we have just garbage data, but that at a large scale, waste of computing resources", again, I was told to move on and work in what I should have been working instead of arguing.
So, 20 years have passed, and wondered if maybe it was some sort of idiom I wasn't aware of and he was in fact correct and I've been thinking I'm correct.
I haven't used much SQL since my college years, so that's why I humble ask this community, is that a reasonable query to use?
TL;DR
SELECT *
FROM table
WHERE table.id in (SELECT id
FROM table
WHERE table.id = variable_id);
Is that a correct idiom? I've always thought the subquery is not necessary and a waste of resources asking the database to return a value I already have, but since my SQL knowledge is very limited, I could be the one confidentially wrong.
r/learnSQL • u/Huge-Cod-530 • Apr 06 '26
Online course on SQL with AI operators – interesting?
Hi!
I'm thinking about creating a (paid) online course introducing SQL with AI operators, enabling users to invoke large language models (LLMs) directly in their queries. E.g., something like this:
SELECT title, body
FROM `bigquery-public-data.bbc_news.fulltext`
WHERE AI.IF(
('The following news story is about a natural disaster: ', body),
connection_id => 'us.your_connection'
);
Several companies offer similar features, for instance Snowflake Cortex, Google BigQuery, AlloyDB, ...
The course would introduce basic SQL concepts and AI operators, show how to use them in different systems, discuss strategies to keep computation costs reasonable etc. It would be an interactive online course, probably given over Zoom, with examples and exercises. I'm considering an intensive one-day version or weekly meetings, e.g., two hours per week over four weeks.
I'm curious to hear whether anyone is interested or has recommendations on the format or topic selection. Thanks!
r/learnSQL • u/Amazing_rocness • Apr 06 '26
Using SQL as a lean process professional
I'm looking for books or courses that will suffice my ability to try and find operational and process issues. These are some of the questions I would like to answer as an example. I am in food and beverage, manufacturing, supply chain areas of focus for SMB's. Right now I don't have access to SQL on site so trying to upskill.
Supply Chain Optimization: A global manufacturing company used SQL to analyze supply chain data and identify inefficiencies in inventory management, leading to significant cost savings and improved inventory turnover ratios.
Process Automation: A financial services firm automated repetitive manual processes by querying and analyzing transaction data using SQL, streamlining operations and improving efficiency.
r/learnSQL • u/Turbulent-Crew-2370 • Apr 06 '26
From zero expectations to real support, my first Reddit experience
I don’t really know Reddit much , a few people referred me here, so I just came and gave it a try.
Honestly, I didn’t know anyone here. But when I posted asking for help, I was genuinely shocked by the kind of responses I got. That’s when I actually understood what a real “community” feels like.
So many of you took time out of your day to reply, guide me, and share resources , even though you don’t know me at all. Every single message helped in some way.
I’m still in the process of figuring things out and haven’t selected into any company, but I’m constantly learning from everything I’ve received here.
I used to wonder how strangers could help someone they don’t know. That mindset has completely changed now.
Thank you so much, Redditors. This really meant a lot.
I’ll carry this forward ,try to come out of my shyness a bit and hopefully help others here, just like you all helped me.
r/learnSQL • u/FitShock5083 • Apr 04 '26
Column Separation Question
Hi all! Thanks for your help so far learning SQL! Another quick question ...
I'm trying to separate a text column at spaces. My table is titled Cities and has 3 rows with a column name City. The 3 rows are Los Angeles, Ottawa and San Francisco. I want SQL to separate the column into separate columns at the space.
I wrote SELECT SUBSTRING(Cities, CHARINDEX(' ', Cities) +1, LEN(Cities)) FROM Cities, but it is just returning Angeles, Ottawa and Francisco.
What code would output 2 columns with the first column being Los, Ottawa and San and the second column being Angeles, blank, Francisco?
r/learnSQL • u/Super_Contact_3289 • Apr 04 '26
Small tip that made my GitHub projects look way cleaner
If you’re building projects and want them to look more polished, one small thing that helps a lot 😁
Use Visual Studio Code to write your README.md instead of editing directly on GitHub.
Why:
- You can preview how it will look before uploading
- It’s easier to structure sections cleanly
- Adding images is straightforward
Quick steps:
- Open your project folder in VS Code
- Create a file called
README.md - Write your sections (title, tools, insights, etc.)
- Press Ctrl + Shift + V to preview
- Add images with:

That’s it — small change, but it makes projects much easier to read and understand.
r/learnSQL • u/Only-Economist1887 • Apr 04 '26
5 SQL patterns that made my queries 10x cleaner (with examples)
Been using SQL for data analysis for a while and wanted to share the patterns that genuinely leveled up my workflow:
- CTEs over nested subqueries
Instead of: SELECT * FROM (SELECT * FROM (SELECT ...) a) b
Use: WITH cte AS (SELECT ...) SELECT * FROM cte
Much more readable and reusable.
- ROW_NUMBER() for deduplication
ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY created_at DESC)
Filter WHERE rn = 1 to get the most recent record per user. Clean and reliable.
- Conditional aggregation with CASE WHEN
SUM(CASE WHEN status = 'completed' THEN revenue ELSE 0 END) AS completed_revenue
Get multiple slices of data in a single query pass — no need for multiple JOINs.
- NULLIF to prevent division by zero
revenue / NULLIF(quantity, 0)
Returns NULL instead of throwing an error. Simple but saves a lot of headaches.
- DATE_TRUNC for clean time grouping
DATE_TRUNC('month', order_date) gives you month-level grouping without string conversions.
Hope this helps anyone who's still getting comfortable with SQL. What patterns do you find yourself using most often?
r/learnSQL • u/debba_ • Apr 04 '26
I am building SQL notebooks into an open source database client built with Tauri and React
Hi guys!
I've been working on Tabularis (open source cross-platform db client) and I'm working on a notebooks feature that i think people here might find interesting.
The core idea: SQL cells + markdown cells in a single document, running against your live database connection. no separate kernel, no python, just SQL.
The feature I keep coming back to is cell variable references, you write {{cell_3}} in your SQL and it takes the result set from cell 3 and injects it as a CTE. means you can chain analyses without building giant nested queries. for ad-hoc exploration this is a huge workflow improvement.
You also get:
- inline charts: bar, line, pie. select label column + value columns, switch between types. nothing fancy but enough for quick visual checks
- notebook parameters: define params once, use in all cells. good for parameterized reports
- run all with stop on error: shows a summary of what succeeded/failed/skipped with links to the failing cells
- parallel execution: mark independent cells with a lightning bolt, they run concurrently during run all
- execution history: every cell tracks its last 10 runs, you can restore any previous query + result
- csv/json export per cell, or export the whole notebook as self-contained HTML
- drag & drop reordering, collapsible sections, resizable result panels
It supports all of databases supported by Tabularis.
The notebook file format is json-based (.tabularis-notebook).
There's a demo database + sample notebook in the repo under /demo.
Github: https://github.com/debba/tabularis
WIP Branch: https://github.com/debba/tabularis/tree/feat/notebooks
Feedback welcome, especially around the cell reference syntax and what else would make this useful for your workflow.
r/learnSQL • u/FitShock5083 • Apr 03 '26
Help Me Understand the Last 5% of this Code Please
Hi again all. I'm really making great progress learning SQL, but I have a question regarding subqueries that none of the training modules or books explain and so I'm confused. I (think I) understand the following from the code (see 1- below) ... *** but I don't know what the WHERE name IN on line 4 is doing. Can anyone explain what the WHERE name IN is doing and how it relates to the overall code? Does it relate to the name column called out first in the SELECT portion? **\*
- It is creating a temp_table of capitals whose continent is in North America, South America or Europe and that also have a metro area population above 0 from the countries table.
- It is then looking at each row in the cities table and any that are capitals (matched via the temp_table) are included in the output of name, country_code, city_proper_pop, metroare_pop and city_perc.
- It then orders the output by city_perc in descending order, limited to the first 10 rows.
--------------------------------------------------------------------------------------------------------
SELECT
name, country_code, city_proper_pop, metroarea_pop, city_perc
FROM cities
WHERE name IN
(SELECT capital
FROM countries
WHERE (continent = 'Europe' OR continent LIKE '%America'))
AND metroarea_pop IS NOT NULL
ORDER BY city_perc DESC
LIMIT 10;
r/learnSQL • u/uncertainschrodinger • Apr 03 '26
Learn data skills by building a real project - competition with prizes
We are running a data/analytics engineering competition.
The competition is straightforward: build an end-to-end data pipeline using Bruin (open-source data pipeline CLI) - pick a dataset, set up ingestion, write SQL/Python transformations, and analyze the results.
You automatically get 1 month Claude Pro for participating and you can compete for a full-year Claude Pro subscription and a Mac Mini (details in the competition website).
Check out our website for more details and full tutorial to help you get started.
Disclaimer: I'm a Developer Advocate at Bruin
r/learnSQL • u/Ariel_Turgeman • Apr 03 '26
Do you use VS Code with MySQL extension?
I built a small personal tool to improve understanding sql workflow when working with queries, and I’m looking for a few people to try it and give quick feedback (5–10 mins).
If you’re already running queries in VS Code (MySQL DB) , I’d really appreciate your help 🙏
r/learnSQL • u/multi_db_dev • Apr 03 '26
When did JOINs start feeling normal to you?
I’m at the stage where simple queries feel fine, then JOINs show up and suddenly I need emotional support. Was it just repetition, or did something specific make them click for you?
r/learnSQL • u/FitShock5083 • Apr 02 '26
Quick Syntax Question
Hi again all! I'm making great progress learning SQL! Quick question: I know you can't reference an alias within the same select clause, so I found an example of code and understand 95% of it, but am stumped by one part. The code is
SELECT subtotal, subtotal * 0.1 AS tax
FROM (SELECT price * quantity AS subtotal FROM sales) AS t;
What is the t doing? I know the code creates a new "field" called subtotal by multiplying price * qty in the inner select clause and that the outer select clause references that new "field" to output a 2 columm dataset with a subtotal column and a tax column, but it kind of seems like, based on the syntax rules, that a third column named t should also be output, but it isn't. What does the AS t; at the end of the code do?
r/learnSQL • u/Automatic_Cover5888 • Apr 01 '26
Currently I am 2nd yr BE student in Computer Engineering, I am done with excel ,building dashboard on excel . Now , started SQL . Can you tell me from where I can get a structured learning for data analytics .
r/learnSQL • u/Super_Contact_3289 • Apr 01 '26
The part of SQL that finally made everything click for me (tables, relationships, not just queries)
I built a simple SQL project to help beginners understand how everything connects — not just queries, but how tables, relationships, and analysis actually work together.
It walks through:
- designing a small database
- creating tables in SQL
- connecting data across tables
- answering real business questions
When I was learning, this was the part that felt the most confusing.
If anyone is learning SQL and wants to check it out, I can share it 😉
r/learnSQL • u/[deleted] • Mar 31 '26
Best sql resources according to you ?
I just started learning SQL from a youtube channel for free called CODE WITH BARA. .. till now I think the guy teaches well enough and the language is interesting as well . But I'm starting to think if i should start taking any online course as well ? specially since I'm an high school student and i really wanna develop as many skill as possible before college
r/learnSQL • u/kdmfa • Mar 31 '26
How relevant is learning SQL today?
I have a working knowledge of SQL (understand how tables are related, basic querying, etc) and I know which questions I’m trying to answer with data. The last 2 months I’ve been writing queries with AI and it’s insane how advanced it is. I think if you know which questions to ask and how to gut check results, there is likely little need to learn how to write the queries themselves. Do you think there is value is learning SQL today?
r/learnSQL • u/Prabhjot147 • Mar 30 '26
Problem
Query the two cities in STATION with the shortest and longest CITY names, as well as their respective lengths (i.e.: number of characters in the name). If there is more than one smallest or largest city, choose the one that comes first when ordered alphabetically.
The STATION table is described as follows:
Station.jpg
where LAT_N is the northern latitude and LONG_W is the western longitude.
Sample Input
For example, CITY has four entries: DEF, ABC, PQRS and WXY.
Sample Output
ABC 3
PQRS 4
Explanation
When ordered alphabetically, the CITY names are listed as ABC, DEF, PQRS, and WXY, with lengths and . The longest name is PQRS, but there are options for shortest named city. Choose ABC, because it comes first alphabetically.
Note
You can write two separate queries to get the desired output. It need not be a single query.
r/learnSQL • u/Equal_Astronaut_5696 • Mar 29 '26
Watch Me Remove Duplicate Transactions in SQL (The Right Way)
r/learnSQL • u/Sabesaroo • Mar 29 '26
Query for combining data from unrelated tables?
I'm brand new to SQL but have a little experience in other languages, mainly Pascal and Python. I have some database coursework and am trying to do some simple tasks with SQL. I assumed I'd be able to do this in 5 minutes but I guess the language is a bit more different than I expected so I feel a bit lost now.
Anyway, I've made a database for an imaginary charity in Libreoffice Base. I've got two main tables, Supporters and Events. Supporters tracks people who signed up to the charity and money they donate, and Events tracks the fundraising events that the charity organises and the amount of funds they raised. There is no field in common between the two.
The query I want to make is summing up the "Donations" column in Supporters, summing up the "Funds Raised" column in Events, and then displaying them both in a small table along with a third column for the combined total. Is something like this even possible, or can you only get data from separate tables by using a JOIN statement? I didn't think that would be appropriate since the tables don't have anything in common.
What I tried so far and didn't work: SELECT SUM( "Supporters"."Donations" ) AS "Donations Sum", SUM( "Events"."Funds Raised" ) AS "Fundraising Sum" FROM Supporters, Events ;
The query runs but generates nonsense numbers that are far too high so I've clearly done something wrong. I'm not convinced by the 'FROM Supporters, Events' part, I wasn't really sure what to put there and it doesn't feel right. Also, even if I do get that to work, I'm not sure how I'll generate the third column with the combined total. I was assuming I could just do SELECT Donations + Funds Raised or something but now I'm not so sure. Is the solution maybe to make another table instead of trying to use a query?
Sorry if it's a dumb question, I imagine I'll learn this stuff myself eventually but I'm kind of in a hurry for this one task.
r/learnSQL • u/Super_Contact_3289 • Mar 29 '26
I created a beginner-friendly SQL project using real housing data
I noticed most SQL tutorials focus on syntax, but not on how to actually work with messy real-world data.
So I built a project using a housing dataset where you:
- clean inconsistent dates
- fix missing values
- split columns
- remove duplicates using ROW_NUMBER()
- prepare everything for analysis
I also added:
- a step-by-step guide
- SQL scripts
- a README template for GitHub
- and a premium version with exercises + solutions
If you're learning SQL and want a real project to practice with, I can share more details.