r/learnSQL • u/Temporary-Cup-2140 • Sep 21 '26
What is one SQL best practise that beginners usually don't know , But is really useful in real projects?
I am learning sql and I know the basic concepts. But I want to know how sql is used in real projects. What is one tip or practice you learned from experience?
63
Upvotes
67
u/Think_Weekend4594 Sep 21 '26
Whenever you run a DELETE query, first run the equivalent SELECT query to verify the data. You don't want to execute a query expecting to delete a couple of rows, only to see the message: "10,000 rows deleted."
23
u/Newcs91 Sep 21 '26
Add comments to your code. You’ll thank yourself later when you’re under pressure to explain why something is calculated the way it is.
Also, construct your JOINS one at a time and check row counts each time. Much easier to spot problems with exploding row counts.
10
u/shine_on Sep 21 '26
You need to know your data. It's all very well writing a query that's valid SQL, but if your where clauses are wrong or you're fundamentally misunderstanding what the data actually means, you're going to get a set of results that looks correct but actually aren't.
Also, pay attention to brackets in where clauses when you're using AND and OR. A AND (B OR C) is different to A AND B OR C
Make sure that people know what they're asking for, you can often be given some extremely vague specifications for a query.
6
u/uncertainschrodinger Sep 21 '26
avoid doing SELECT DISTINCT - instead do an explicit deduplication in a previous step/cte so that someone reading the code code can understand the granularity by reading the dedup logic
for pipelines or scheduled runs, learn how to create quality checks and unit tests, this helps:
- test the logic and outcome of the query
- make it easier for someone reading/updating the query to understand the purpose and logic
lastly, I'd recommend also learning and getting comfortable with using AI because thats how sql is written and maintained these days - this means learning how to set up the environment and context, how to prompt the agent, and how to review the queries and results
5
u/First-Feature-3556 Sep 21 '26
Everyone tells you to use stored procedures when developing software. Nobody tells you that they come at a cost.
The cost is that your business logic is now split between your actual code (C#, Java, whatever) and your database, and you need to keep those two in sync. That can be worthwhile, if that extra bit of performance is really needed, but if it isn't, you just made version control, maintenance and deployment a lot more complicated.
4
u/jeffrey_f Sep 21 '26
Users get STORED PROCEDURES/Queries and no direct access to data, except through a process that IT gives them. Prevent data corruption.
3
u/Designer_Emu_6518 Sep 21 '26
Learn lingo and surface knowledge of correct output as ai will be really good at this aspect in like a year
3
u/KittyTheBandit Sep 21 '26
Not a best practice but using COLLECT_SET() and then ARRAY_CONTAINS() is very handy if you're SQL environment allows it.
2
u/SektorL Sep 27 '26
Use window functions. Once you master them, there's no way back.
2
u/markinatlanta Sep 22 '26
Check your row counts before and after a JOIN. If you start with 1000 orders and end up with 1400 rows for example, then understand why before addin up revenue.
One order matching multiple rows in another table can inflate totals completely that makes it very easy to miss
2
u/TomWickerath Sep 25 '26
Learn to read execution plans and understand performance warnings such as spilling to disk, implicit data conversions (can’t use index), missing statistics, excessive memory grants, etc.
1
u/Wuthering_depths Sep 21 '26 edited Sep 21 '26
Not sure how mysterious this is for everyone else, but I'd say derived tables. These were something I didn't know about for quite a while. I've seen sql writers tie themselves in knots with the rest of a query when a query in the FROM (aka derived table) can solve the issue.
Another thing I've used a ton of in recent years that I went a long time before discovering: UNION subqueries. Two or more unioned queries wrapped in an aliased outer query.
1
u/peperinna Sep 21 '26
Evitar SELECT *, al comienzo es fácil, pero cuando tienes ya muchos datos, el procesamiento consume bastante. Aprende a usar LIMIT
1
u/jwright4105 Sep 22 '26
Begin work;
Delete from table where xxxx;
—Commit Work;
—Rollback Work;
1
u/Kekos3some Sep 24 '26
Disable Autocommit.
1
u/Kekos3some Sep 24 '26
Always use Explicit Joins (ANSI SQL-92)
1
u/elevarq Sep 27 '26
Read the error messages! Your observation, "something went wrong," often becomes knowledge about what you did wrong, and you learn from it.
Beginners (in any programming language) have a bad habit of ignoring this valuable information.