SQL Formatter

🗃️ You are on the SQL Formatter page

Format and beautify your SQL queries instantly. Supports validation, syntax highlighting, and download.

Input SQL

1

Result

1

What is SQL?

SQL (Structured Query Language) is a standardized programming language designed for managing and manipulating relational databases. SQL is used to create, modify, and query databases, making it essential for data management and analysis.

Key Features of SQL

  • Data Querying: Retrieve specific data from databases using SELECT statements.
  • Data Manipulation: Insert, update, and delete data using DML commands.
  • Schema Definition: Create and modify database structures with DDL commands.
  • Data Control: Manage user permissions and access control with DCL commands.
  • Standardized: ANSI/ISO standard ensures compatibility across database systems.
  • Declarative: Specify what you want, not how to get it.

SQL Categories

  • DQL (Data Query Language): SELECT statements for data retrieval.
  • DML (Data Manipulation Language): INSERT, UPDATE, DELETE operations.
  • DDL (Data Definition Language): CREATE, ALTER, DROP statements.
  • DCL (Data Control Language): GRANT, REVOKE permissions.
  • TCL (Transaction Control Language): COMMIT, ROLLBACK, SAVEPOINT.

Example of SQL Query

-- Customer Analysis Query
-- This query demonstrates various SQL features including joins, aggregations, and window functions

WITH customer_stats AS (
    SELECT 
        c.customer_id,
        c.first_name,
        c.last_name,
        c.email,
        c.registration_date,
        COUNT(o.order_id) AS total_orders,
        SUM(o.total_amount) AS total_spent,
        AVG(o.total_amount) AS avg_order_value,
        MAX(o.order_date) AS last_order_date,
        MIN(o.order_date) AS first_order_date,
        ROW_NUMBER() OVER (ORDER BY SUM(o.total_amount) DESC) AS spending_rank
    FROM 
        customers c
    LEFT JOIN 
        orders o ON c.customer_id = o.customer_id
    WHERE 
        c.status = 'active'
        AND c.registration_date >= '2023-01-01'
    GROUP BY 
        c.customer_id, 
        c.first_name, 
        c.last_name, 
        c.email, 
        c.registration_date
    HAVING 
        COUNT(o.order_id) > 0
),
category_preferences AS (
    SELECT 
        o.customer_id,
        p.category,
        COUNT(*) AS category_orders,
        SUM(oi.quantity * oi.unit_price) AS category_spending,
        RANK() OVER (
            PARTITION BY o.customer_id 
            ORDER BY COUNT(*) DESC
        ) AS preference_rank
    FROM 
        orders o
    INNER JOIN 
        order_items oi ON o.order_id = oi.order_id
    INNER JOIN 
        products p ON oi.product_id = p.product_id
    WHERE 
        o.order_date >= CURRENT_DATE - INTERVAL '1 year'
    GROUP BY 
        o.customer_id, p.category
)
SELECT 
    cs.customer_id,
    cs.first_name || ' ' || cs.last_name AS full_name,
    cs.email,
    cs.total_orders,
    ROUND(cs.total_spent, 2) AS total_spent,
    ROUND(cs.avg_order_value, 2) AS avg_order_value,
    cs.last_order_date,
    cs.spending_rank,
    cp.category AS preferred_category,
    cp.category_orders,
    ROUND(cp.category_spending, 2) AS category_spending,
    CASE 
        WHEN cs.total_spent >= 1000 THEN 'VIP'
        WHEN cs.total_spent >= 500 THEN 'Premium'
        WHEN cs.total_spent >= 100 THEN 'Regular'
        ELSE 'New'
    END AS customer_tier,
    CASE 
        WHEN cs.last_order_date >= CURRENT_DATE - INTERVAL '30 days' THEN 'Active'
        WHEN cs.last_order_date >= CURRENT_DATE - INTERVAL '90 days' THEN 'Inactive'
        ELSE 'Churned'
    END AS customer_status,
    DATEDIFF(cs.last_order_date, cs.first_order_date) AS customer_lifetime_days
FROM 
    customer_stats cs
LEFT JOIN 
    category_preferences cp ON cs.customer_id = cp.customer_id 
    AND cp.preference_rank = 1
WHERE 
    cs.spending_rank <= 100  -- Top 100 customers by spending
ORDER BY 
    cs.total_spent DESC,
    cs.last_order_date DESC
LIMIT 50;

Explanation:
- Uses Common Table Expressions (CTEs) for complex data organization.
- Demonstrates JOINs between multiple tables (customers, orders, products).
- Includes aggregate functions (COUNT, SUM, AVG, MAX, MIN).
- Window functions (ROW_NUMBER, RANK, PARTITION BY) for advanced analytics.
- CASE statements for conditional logic and data categorization.
- Date functions and interval calculations for time-based analysis.

Advantages of SQL

  • Standardization: Widely accepted standard across different database systems.
  • Declarative: Focus on what data you need, not how to retrieve it.
  • Powerful: Complex queries with joins, subqueries, and aggregations.
  • Data Integrity: Built-in constraints and transaction support.
  • Scalability: Efficient handling of large datasets with optimization.
  • Security: Role-based access control and permission management.

Common SQL Commands

Data Query (DQL):

SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY

Data Manipulation (DML):

INSERT, UPDATE, DELETE

Data Definition (DDL):

CREATE, ALTER, DROP, TRUNCATE

Data Control (DCL):

GRANT, REVOKE

Transaction Control (TCL):

COMMIT, ROLLBACK, SAVEPOINT

SQL vs. NoSQL Databases

FeatureSQL (Relational)NoSQL
Data StructureTables with rows/columnsDocuments, key-value, graph
SchemaFixed schemaDynamic schema
ACID PropertiesStrong ACID complianceEventually consistent
ScalingVertical scalingHorizontal scaling
Query LanguageSQLVaries by database
Use CasesComplex transactionsBig data, real-time apps

Frequently Asked Questions (FAQs)

1. What does SQL stand for?

SQL stands for Structured Query Language, a standard language for managing relational databases.

2. Is SQL case-sensitive?

SQL keywords are generally not case-sensitive, but database and table names may be depending on the database system.

3. What's the difference between WHERE and HAVING?

WHERE filters rows before grouping, while HAVING filters groups after GROUP BY aggregation.

4. What are SQL joins?

Joins combine data from multiple tables based on related columns: INNER, LEFT, RIGHT, FULL OUTER joins.

5. How do I optimize SQL query performance?

Use indexes, limit result sets, avoid SELECT *, use proper WHERE clauses, and analyze execution plans.

6. What are SQL constraints?

Rules enforced on data: PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL, CHECK constraints.

7. What's a subquery in SQL?

A query nested inside another query, used in WHERE, SELECT, or FROM clauses for complex filtering.

8. How do SQL transactions work?

Transactions ensure data consistency with ACID properties: Atomicity, Consistency, Isolation, Durability.

9. What are window functions in SQL?

Advanced functions that perform calculations across related rows: ROW_NUMBER(), RANK(), LAG(), LEAD().

10. Which SQL databases are most popular?

MySQL, PostgreSQL, SQL Server, Oracle, SQLite are among the most widely used SQL databases.

Conclusion

SQL remains the cornerstone of data management in modern applications. Its powerful query capabilities, standardization, and robust ecosystem make it essential for developers, analysts, and data professionals working with relational databases.