Skip to main content

Master the Top SQL Queries: Clear Explanations + Practical Examples

Updated 5 min readMarkdown
Part of seriesNode.js Mastery: From Basics to Real-world Projects

1️⃣ SELECT (Basic Data Fetching)

🔹 What it does:

Fetch data from a table.

🔹 Syntax:

SELECT column1, column2 FROM table_name;

🔹 Example:

SELECT name, salary FROM employees;

🔹 Use Case:

  • Showing user list

  • Displaying products

  • Fetching dashboard data


2️⃣ WHERE (Filtering Data)

🔹 What it does:

Filters rows based on condition.

SELECT * FROM employees WHERE salary > 50000;

🔹 Use Case:

  • Active users only

  • Orders of specific customer

  • Filter by date


3️⃣ ORDER BY (Sorting Data)

🔹 What it does:

Sorts result in ASC or DESC order.

SELECT * FROM employees ORDER BY salary DESC;

🔹 Use Case:

  • Top 10 highest salary

  • Latest orders first

  • Ranking system


4️⃣ LIMIT (Pagination)

🔹 What it does:

Limits number of records.

SELECT * FROM employees LIMIT 5;

🔹 Use Case:

  • Pagination

  • Dashboard preview

  • Showing top records


5️⃣ COUNT()

🔹 What it does:

Counts number of rows.

SELECT COUNT(*) FROM employees;

🔹 Use Case:

  • Total users count

  • Total orders

  • Analytics


6️⃣ GROUP BY

🔹 What it does:

Groups rows by column.

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

🔹 Use Case:

  • Department wise employees

  • Category wise products

  • Monthly reports


7️⃣ HAVING

🔹 What it does:

Filters grouped data.

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

🔹 Use Case:

  • Departments having more than 5 employees

  • Customers with more than 10 orders


8️⃣ INNER JOIN (Very Important 🔥)

🔹 What it does:

Returns matching records from two tables.

SELECT users.name, orders.amount
FROM users
INNER JOIN orders 
ON users.id = orders.user_id;

🔹 Real-Life Example:

User table + Orders table combine karna

🔹 Use Case:

  • Show customer order history

  • Join product & category table


9️⃣ LEFT JOIN

🔹 What it does:

Returns all records from left table + matching from right.

SELECT users.name, orders.amount
FROM users
LEFT JOIN orders 
ON users.id = orders.user_id;

🔹 Use Case:

  • Show all users even if they haven’t placed order

🔟 INSERT

🔹 What it does:

Adds new record.

INSERT INTO employees (name, salary) 
VALUES ('Rahul', 60000);

🔹 Use Case:

  • User registration

  • Add product

  • Add order


1️⃣1️⃣ UPDATE

🔹 What it does:

Modify existing data.

UPDATE employees 
SET salary = 70000 
WHERE id = 1;

🔹 Use Case:

  • Update profile

  • Change order status

  • Modify price


1️⃣2️⃣ DELETE

🔹 What it does:

Delete records.

DELETE FROM employees 
WHERE id = 5;

🔹 Use Case:

  • Delete account

  • Remove product

  • Cancel order


1️⃣3️⃣ DISTINCT

🔹 What it does:

Removes duplicate values.

SELECT DISTINCT department FROM employees;

🔹 Use Case:

  • Unique categories

  • Unique cities list


1️⃣4️⃣ BETWEEN

SELECT * FROM employees 
WHERE salary BETWEEN 30000 AND 60000;

🔹 Use Case:

  • Salary range filter

  • Date range filter


1️⃣5️⃣ IN

SELECT * FROM employees 
WHERE department IN ('HR', 'IT');

🔹 Use Case:

  • Multiple filter values

  • Category selection


1️⃣6️⃣ LIKE (Pattern Matching)

SELECT * FROM users 
WHERE name LIKE 'P%';

🔹 Use Case:

  • Search functionality

  • Autocomplete

  • Filter by keyword


1️⃣7️⃣ Subquery (Advanced 🔥)

SELECT name FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);

🔹 Use Case:

  • Above average salary employees

  • Complex filtering


1️⃣8️⃣ MAX() / MIN() / AVG() / SUM()

SELECT MAX(salary) FROM employees;
SELECT AVG(salary) FROM employees;

🔹 Use Case:

  • Analytics

  • Reports

  • KPI dashboard


1️⃣9️⃣ CASE (Conditional Logic)

SELECT name,
CASE 
    WHEN salary > 50000 THEN 'High'
    ELSE 'Low'
END AS salary_level
FROM employees;

🔹 Use Case:

  • Status labeling

  • Custom categorization


2️⃣0️⃣ Index (Performance Optimization 🔥)

CREATE INDEX idx_salary ON employees(salary);

🔹 What it does:

Speeds up search queries.

🔹 Use Case:

  • Large tables

  • Frequently searched columns


🎯 BONUS: Most Asked Interview Query

👉 Find 2nd Highest Salary

SELECT MAX(salary) 
FROM employees 
WHERE salary < (SELECT MAX(salary) FROM employees);

🚀 Real Interview Focus Topics (Must Prepare)

These are HIGHLY important:

  • Joins (all types)

  • Group By + Having

  • Subqueries

  • Indexing

  • Normalization

  • Transactions

  • Primary key vs Unique key

  • Stored procedures

  • Views

  • Triggers


💡Since You Work With MySQL + Backend

You should also practice:

  • Pagination query using LIMIT + OFFSET

  • API filtering queries

  • Soft delete concept (is_deleted = 1)

  • Date filtering (DATE(), NOW())

  • Query optimization

    🔔 Stay Connected

    If you found this article helpful and want to receive more such beginner-friendly and industry-relevant Node JS notes, tutorials, and project ideas — 📩 Subscribe to our newsletter by entering your email below.

    And if you're someone who wants to prepare for tech interviews while having a little fun and entertainment, 🎥 Don’t forget to subscribe to my YouTube channel – Knowledge Factory 22 – for regular content on tech concepts, career tips, and coding insights!

    Stay curious. Keep building. 🚀

About the author

  • Hi there, tech enthusiasts! I'm a passionate Software Developer driven by a love for continuous learning and innovation. I thrive on exploring new tools and technologies, pushing boundaries, and finding creative solutions to complex problems. What You'll Find Here On my Hashnode blog, I share: 🚀 In-depth explorations of emerging technologies 💡 Practical tutorials and how-to guides 🔧Insights on software development best practices 🚀Reviews of the latest tools and frameworks 💡 Personal experiences from real-world projects. Join me as we bridge imagination and implementation in the tech world. Whether you're a seasoned pro or just starting out, there's always something new to discover! Let’s connect and grow together! 🌟

No comments yet. Be the first to comment.