Skip to content
AgileLogics
3 min read

Comments and SQL Style

Prerequisites: Your first query: SELECT

Comments

-- A single-line comment
SELECT name   -- a comment after code
FROM customers;

/* A comment
   spanning several lines */

Use comments to explain why a query does something unusual — business rules, known data problems, why a filter exists:

-- Exclude test accounts created by the QA team (emails end in @test.local)
SELECT COUNT(*) FROM customers WHERE email NOT LIKE '%@test.local';

Readable style

There's no single official style, but these habits are widely shared:

  • Keywords in uppercase, names in lowercase snake_case.
  • One clause per line, with the main keywords (SELECT, FROM, WHERE, GROUP BY) lined up.
  • Meaningful aliases: AS order_total, not AS x.
  • Trailing or leading commas — pick one and be consistent.

Compare:

select category,count(*),avg(price) from products where price>30 group by category order by 2 desc;
SELECT
  category,
  COUNT(*)   AS products,
  AVG(price) AS avg_price
FROM products
WHERE price > 30
GROUP BY category
ORDER BY products DESC;

Both work; the second is far easier to read, review, and change. The SQL Formatter reformats messy queries for you, and linters such as SQLFluff enforce a team style automatically.

What's next

You now know enough SQL to answer real questions about a single table. Try the Beginner Projects, then continue to Level 2 — Intermediate, which starts with joins.