MYSQL
- What are the differences between SQL and MySQL?
- What are the different data types in MySQL?
- Explain MySQL storage engines.
- What is the difference between MyISAM and InnoDB?
- What is a Primary key vs Foreign key
- What are constraints in MySQL?
- Explain the difference between CHAR and VARCHAR.
- What is normalization? Explain different normal forms.
- What is denormalization?
- What is the difference between WHERE and HAVING?
- Joins in mysql
- What is the difference between UNION and UNION ALL?
- What is a composite key?
- What is EXPLAIN in MySQL?
- MySQL Indexing & Performance: A Complete Guide
- What are aggregate functions? Give examples.
- What is COALESCE() function?
- Understanding Transactions in Databases: A Complete Guide
- What are different types of locks in MySQL?
- Stored Procedures and Triggers in MySQL: Complete Guide
- Difference between a trigger and stored procedure?
- Views in MySQL β A Complete Guide
- What are temporary tables?
- MySQL Replication: A Complete Guide
- Partitioning in MySQL: A Complete Guide
- Difference between MySQL and MariaDB?
- How to monitor MySQL performance?
- MySQL Event Scheduler β The Complete Guide
- Understanding ROW_FORMAT in InnoDB (MySQL)
- What Are Triggers in MySQL? A Complete Guide
- Understanding Partitioning in Databases: A Complete Guide
- Difference between horizontal and vertical partitioning?
- What is replication in MySQL?
- Difference between master-slave and master-master replication?
- What is referential integrity?
- Difference Between CHAR, VARCHAR, and TEXT in MySQL
- Difference between ENUM and SET.
- What is a cursor in MySQL?
- Difference between temporary and regular table?
- Understanding ACID Properties in Databases
- What is query caching?
- IN, OUT, and INOUT parameters mean in Stored Procedures (MySQL)
- Optimistic vs Pessimistic Locking in MySQL
- Deadlocks in MySQL β Causes, Detection & Prevention
- Phantom Reads, Dirty Reads & Non-Repeatable Reads Explained
- MySQL Date and Time Basic Questions
- Dynamic period queries in MySQL for last week, last month, this year, etc.
- π₯ Real-World Scenario-Based Date-Time Questions in MySQL
- π₯ Basic Date-Time Questions in MySQL
- π₯ Medium Level Date-Time Questions in MySQL
- π₯ Advanced Level Date-Time Questions in MySQL
What is the difference between WHERE and HAVING?
The difference between WHERE and HAVING in SQL is subtle but important. Letβs break it down clearly:
| Aspect | WHERE | HAVING |
|---|---|---|
| Purpose | Filters rows before any grouping. | Filters groups after aggregation (after GROUP BY). |
| Used With | Works on individual row conditions. | Works on aggregated data (like SUM(), COUNT(), AVG()). |
| Aggregation Functions | Cannot use aggregation functions like SUM() or COUNT() directly. | Can use aggregation functions directly. |
| Execution Order | Applied before GROUP BY. | Applied after GROUP BY. |
Example:
Suppose we have a sales table:
| id | product | amount |
|---|---|---|
| 1 | A | 100 |
| 2 | B | 200 |
| 3 | A | 150 |
| 4 | B | 50 |
Using WHERE: Filter rows where amount > 100:
SELECT * FROM sales
WHERE amount > 100;
Result:
| id | product | amount |
|---|---|---|
| 2 | B | 200 |
| 3 | A | 150 |
Using HAVING: Find products whose total sales > 200:
SELECT product, SUM(amount) as total_sales
FROM sales
GROUP BY product
HAVING SUM(amount) > 200;
Result:
| product | total_sales |
|---|---|
| A | 250 |
β Key takeaway:
WHEREβ row-level filteringHAVINGβ group-level filtering
No comments yet! You be the first to comment.
