10 SQL Queries Every Data Analyst Should Know

SQL (Structured Query Language) is the backbone of data analysis. It helps us talk to databases and get the data we need. Whether you want to find out the total sales, the top products, or just clean up wrong data, SQL is your go-to tool.

If you are a data analyst or want to become one, knowing these 10 SQL queries will make your life super easy. Let’s break down each query with simple language, easy syntax, examples, and where you would use them.

SQL Queries

10 SQL Queries Every Data Analyst Should Know

  1. 1. SELECT — Fetch Data from Database

SELECT is used to choose and display data from a database table.

Syntax:

SELECT column1, column2 FROM table_name;

Example:

SELECT name, salary FROM employees;

Use: Use SELECT when you want to see specific data like employee names or sales amounts.

  1. 2. WHERE — Filter Data Based on Conditions

WHERE helps you filter data to show only rows that meet certain criteria.

Syntax:

SELECT * FROM table_name WHERE condition;

Example:

SELECT * FROM employees WHERE salary > 50000;

Use: Use WHERE to find data that matches your question, like employees earning more than 50,000.

  1. 3. GROUP BY and Aggregate Functions (COUNT, SUM, AVG, MAX, MIN)

GROUP BY groups rows that have the same values. Aggregate functions like COUNT, SUM, AVG, MAX, MIN summarize data.

Syntax:

SELECT column, COUNT(*)

FROM table_name

GROUP BY column;

Example:

SELECT department, COUNT(*) FROM employees GROUP BY department;

Use: Use GROUP BY with COUNT to find how many employees are in each department, or SUM to get total sales per product.

  1. 4. JOIN — Combine Data from Multiple Tables

JOIN combines rows from two or more tables based on related columns.

Syntax:

SELECT columns

FROM table1

JOIN table2 ON table1.common_column = table2.common_column;

Example:

SELECT employees.name, departments.department_name

FROM employees

JOIN departments ON employees.department_id = departments.id;

Use: Use JOIN to get complete information when data is split in different tables, like employee info and department details.

  1. 5. ORDER BY — Sort Data

ORDER BY sorts the results in ascending (ASC) or descending (DESC) order.

Syntax:

SELECT * FROM table_name ORDER BY column ASC|DESC;

Example:

SELECT name, salary FROM employees ORDER BY salary DESC;

Use: Use ORDER BY to see top-performing products or highest-paid employees.

  1. 6. LIMIT — Restrict Number of Rows Returned

LIMIT restricts the number of rows returned in the query result.

Syntax:

SELECT * FROM table_name LIMIT number;

Example:

SELECT * FROM employees LIMIT 5;

Use: Use LIMIT when you only want to see the first few records, like top 5 sales.

  1. 7. SUBQUERIES — Query Inside a Query

A subquery is a query inside another SQL query to help filter or calculate data.

Syntax:

SELECT * FROM table_name WHERE column = (SELECT column FROM another_table WHERE condition);

Example:

SELECT name FROM employees WHERE department_id = (SELECT id FROM departments WHERE department_name = ‘Sales’);

Use: Use subqueries to find data based on complex conditions, like employees working in the Sales department.

  1. 8. CASE — Conditional Logic

CASE lets you perform if-else logic inside your query.

Syntax:

SELECT column,

    CASE

        WHEN condition1 THEN result1

        WHEN condition2 THEN result2

        ELSE result3

    END as new_column

FROM table_name;

Example:

SELECT name, salary,

    CASE

        WHEN salary > 70000 THEN ‘High’

        WHEN salary BETWEEN 40000 AND 70000 THEN ‘Medium’

        ELSE ‘Low’

    END as salary_grade

FROM employees;

Use: Use CASE to categorize data, like salary groups or customer ratings.

  1. 9. DISTINCT — Get Unique Values

DISTINCT removes duplicate rows in the result set.

Syntax:

SELECT DISTINCT column FROM table_name;

Example:

SELECT DISTINCT department FROM employees;

Use: Use DISTINCT to find unique values, like all different departments in a company.

  1. 10. UPDATE & DELETE — Modify or Remove Data

UPDATE changes existing data. DELETE removes data from a table.

Syntax UPDATE:

UPDATE table_name SET column = value WHERE condition;

Syntax DELETE:

DELETE FROM table_name WHERE condition;

Example UPDATE:

UPDATE employees SET salary = 60000 WHERE name = ‘Rahul’;

Example DELETE:

DELETE FROM employees WHERE salary < 20000;

Use: Use UPDATE to correct or change information, like updating salaries. Use DELETE to remove incorrect or unwanted data.

Final Thought:

SQL is the backbone of data analysis. Once you understand these 10 queries, you’ll be able to handle most daily tasks a data analyst faces from cleaning and filtering data to summarizing and reporting insights. The best part is SQL is easy to learn, widely used, and very powerful.

At ConsoleFlare, we believe in teaching data skills that are simple, practical, and industry-ready. Our training helps beginners, students, and professionals become confident data analysts who can handle real-world data challenges with ease. So, start with these 10 SQL queries and build a strong foundation for your data career!

If you want to grow your data skills and learn more about SQL and data analysis, ConsoleFlare is here to guide you step-by-step.

For more such content and regular updates, follow us on FacebookInstagramLinkedIn

seoadmin

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top