Format a SQL SELECT with JOIN
Multi-table JOIN queries quickly become hard to read when written on a single line or with inconsistent indentation. This example joins a users table with orders and applies filtering and sorting. The SQL formatter applies consistent keyword capitalization, indentation for JOIN conditions, and line breaks that match standard SQL style guides. Use formatted SQL in code reviews, documentation, and onboarding materials.
Example
SELECT u.id, u.name, u.email, COUNT(o.id) AS order_count, SUM(o.total) AS total_spent FROM users u INNER JOIN orders o ON u.id = o.user_id INNER JOIN order_status os ON o.status_id = os.id WHERE u.active = 1 AND os.name != 'cancelled' GROUP BY u.id, u.name, u.email ORDER BY total_spent DESC LIMIT 20;
FAQ
- What is the difference between INNER JOIN and LEFT JOIN?
- INNER JOIN returns only rows where the join condition matches in both tables. LEFT JOIN returns all rows from the left table plus matching rows from the right, with NULLs where there is no match.
- Should SQL keywords be uppercase?
- By convention, SQL keywords like SELECT, FROM, WHERE, and JOIN are written in uppercase while table and column names use lowercase or snake_case. The formatter enforces this convention.
- What does aliasing do in SQL?
- Aliases (AS or implicit with a space) give tables and columns shorter names within a query. Table aliases like u for users reduce repetition in JOIN conditions and SELECT lists.
Related Examples
Format a SQL GROUP BY with Aggregates
Aggregate queries with GROUP BY and HAVING are the backbone of reporting and ana...
Format a SQL UPDATE with WHEREUPDATE statements with multiple columns and compound WHERE conditions are easy t...
Format a SQL CREATE TABLE StatementCREATE TABLE statements define the schema of your database and should be readabl...