SQL for Data Analysis

    Yash Kabra3 min readUpdated

    SQL (Structured Query Language) is how you ask questions of data stored in databases. For analysts it is arguably the single most important skill, because nearly all company data lives in databases and SQL is the universal way to retrieve it. The core is small: SELECT what you want, FROM a table, WHERE a condition holds, GROUP BY a category, and JOIN related tables together.

    Reading data: SELECT and WHERE

    -- Select specific columns from a table
    SELECT name, city, marks
    FROM students;
    
    -- Filter rows with WHERE
    SELECT name, marks
    FROM students
    WHERE city = 'Jalgaon' AND marks > 70;
    

    SELECT * returns all columns, but naming the columns you need is clearer and faster.

    Sorting and limiting

    -- Top 5 students by marks, highest first
    SELECT name, marks
    FROM students
    ORDER BY marks DESC
    LIMIT 5;
    

    Aggregating with GROUP BY

    This is where SQL shines for analytics: summarising data by category.

    -- Average marks per city
    SELECT city, AVG(marks) AS avg_marks, COUNT(*) AS num_students
    FROM students
    GROUP BY city;
    

    Common aggregate functions are COUNT, SUM, AVG, MIN, and MAX. To filter on an aggregate, use HAVING (not WHERE):

    -- Only cities with more than 10 students
    SELECT city, COUNT(*) AS num_students
    FROM students
    GROUP BY city
    HAVING COUNT(*) > 10;
    

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses

    Joining tables

    Real databases split data across tables. A JOIN recombines them on a shared key.

    -- Combine orders with customer names
    SELECT o.order_id, c.name, o.amount
    FROM orders AS o
    INNER JOIN customers AS c
      ON o.customer_id = c.id;
    
    • INNER JOIN keeps only rows that match in both tables.
    • LEFT JOIN keeps all rows from the left table, filling unmatched right-side columns with NULL.

    A complete analytics query

    -- Total revenue per city, only counting completed orders,
    -- showing the highest-earning cities first
    SELECT c.city, SUM(o.amount) AS total_revenue
    FROM orders AS o
    LEFT JOIN customers AS c
      ON o.customer_id = c.id
    WHERE o.status = 'completed'
    GROUP BY c.city
    ORDER BY total_revenue DESC;
    

    That single query filters, joins, groups, aggregates, and sorts — the full analytics toolkit in one statement. SQL and pandas share the same concepts, so learning one helps the other.

    Common mistakes

    • Using WHERE on aggregates. Filter aggregated values with HAVING, not WHERE. WHERE filters individual rows before grouping.
    • Forgetting the JOIN condition. Omitting ON produces a huge, meaningless cross-join.
    • Selecting non-grouped columns. Every selected column must either be in GROUP BY or wrapped in an aggregate function.
    • Assuming row order. Without ORDER BY, databases may return rows in any order.

    FAQ

    Is SQL hard to learn? No — the basics above cover most analyst work, and the syntax reads almost like English.

    Which database should I practise on? Any will do. PostgreSQL, MySQL, and SQLite all use standard SQL for the operations covered here.

    Do data scientists use SQL too? Yes, constantly. It is a shared skill across the whole field, as our data science vs analytics guide explains.

    Keep learning

    SQL unlocks the data that lives in databases. Combine it with the rest of the analyst toolkit in the Data Science & Analytics hub.

    Want guided SQL practice on realistic datasets? Join the waitlist for the Data Science & Analytics course at Infoplanet, Jalgaon.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses
    Yash Kabra

    Founder, Atlee Technologies

    Yash Kabra is the founder of Atlee Technologies, a product studio that ships SaaS products end-to-end. He owns products from strategy through launch and growth — including Infoplanet, TrackRise and Perqee — and teaches AI, Machine Learning and Data Science at Infoplanet with a focus on how these tools are used to build real products.

    Related guides