Complete SQL Syllabus with Examples (Colored)
How SQL Code Should Look Like
-- This section explains how good SQL looks like in terms of formatting and syntax
-- - Example 1: Clean SELECT with CTE
WITH high_earners AS (
SELECT
emp_id,
name,
salary
FROM employees
WHERE salary > 70000
)
SELECT *
FROM high_earners;
-- - Example 2: Proper JOIN with aliases
SELECT
e.name,
d.department_name,
e.salary
FROM employees e
JOIN departments d ON e.department_id = d.department_id
WHERE e.salary > 60000;
-- - Example 3: Using window functions with formatting
WITH ranked_orders AS (
SELECT
emp_id,
order_amount,
RANK() OVER (PARTITION BY emp_id ORDER BY order_amount DESC) AS order_rank
FROM orders
)
SELECT * FROM ranked_orders WHERE order_rank = 1;
-- - Formatting Tips:
-- - Always capitalize SQL keywords (SELECT, FROM, WHERE, etc.)
-- - Break long queries into readable lines
-- - Use indentation inside CTEs or subqueries
-- - Comment your logic with --
-- - Use aliases for tables when joining multiple sources
-- - Separate clauses like WHERE, GROUP BY, ORDER BY onto new lines
-- - Visual Style (as seen in most editors)
-- Keywords: SELECT, FROM, WHERE --> Blue
-- Strings: 'Sales', '2023-01-01' --> Green or Orange
-- Functions: COUNT(), AVG(), RANK() --> Purple
-- Column names: salary, department_id --> Black or Default
-- Aliases: e, d --> Black or Grey
Complete SQL Syllabus with Examples (Colored)
-- This formatting not only looks professional but also makes code easier to debug and maintain.