Conditional expressions in SQL allow you to perform different actions based on certain conditions. They are essential for writing complex queries that require decision-making logic. In this section, we will cover the following key concepts:
- CASE Statement
- IF Statement
- COALESCE Function
- NULLIF Function
- CASE Statement
The CASE statement is a powerful tool for adding conditional logic to your SQL queries. It allows you to return different values based on specified conditions.
Syntax
Example
Let's consider a table employees with the following structure:
| employee_id | name | salary | department |
|---|---|---|---|
| 1 | Alice | 50000 | HR |
| 2 | Bob | 60000 | IT |
| 3 | Charlie | 55000 | HR |
| 4 | David | 70000 | IT |
| 5 | Eve | 65000 | Finance |
We want to categorize employees based on their salary:
SELECT name,
salary,
CASE
WHEN salary < 55000 THEN 'Low'
WHEN salary BETWEEN 55000 AND 65000 THEN 'Medium'
ELSE 'High'
END AS salary_category
FROM employees;Explanation
- WHEN salary < 55000 THEN 'Low': If the salary is less than 55000, the category is 'Low'.
- WHEN salary BETWEEN 55000 AND 65000 THEN 'Medium': If the salary is between 55000 and 65000, the category is 'Medium'.
- ELSE 'High': For all other cases, the category is 'High'.
Result
| name | salary | salary_category |
|---|---|---|
| Alice | 50000 | Low |
| Bob | 60000 | Medium |
| Charlie | 55000 | Medium |
| David | 70000 | High |
| Eve | 65000 | Medium |
- IF Statement
The IF statement is another way to add conditional logic, but it is less commonly used in standard SQL and more often found in specific SQL dialects like MySQL.
Syntax
Example
Using the same employees table, we can categorize employees based on their department:
SELECT name,
department,
IF(department = 'IT', 'Tech', 'Non-Tech') AS department_category
FROM employees;Explanation
- IF(department = 'IT', 'Tech', 'Non-Tech'): If the department is 'IT', the category is 'Tech'. Otherwise, it is 'Non-Tech'.
Result
| name | department | department_category |
|---|---|---|
| Alice | HR | Non-Tech |
| Bob | IT | Tech |
| Charlie | HR | Non-Tech |
| David | IT | Tech |
| Eve | Finance | Non-Tech |
- COALESCE Function
The COALESCE function returns the first non-null value in a list of expressions. It is useful for handling null values.
Syntax
Example
Consider a table orders with the following structure:
| order_id | customer_id | discount |
|---|---|---|
| 1 | 101 | NULL |
| 2 | 102 | 10 |
| 3 | 103 | NULL |
| 4 | 104 | 5 |
We want to ensure that all null discounts are treated as zero:
Explanation
- COALESCE(discount, 0): If the discount is null, it returns 0. Otherwise, it returns the discount value.
Result
| order_id | customer_id | discount |
|---|---|---|
| 1 | 101 | 0 |
| 2 | 102 | 10 |
| 3 | 103 | 0 |
| 4 | 104 | 5 |
- NULLIF Function
The NULLIF function returns null if the two specified expressions are equal. Otherwise, it returns the first expression.
Syntax
Example
Using the same orders table, we want to set the discount to null if it is zero:
Explanation
- NULLIF(discount, 0): If the discount is 0, it returns null. Otherwise, it returns the discount value.
Result
| order_id | customer_id | discount |
|---|---|---|
| 1 | 101 | NULL |
| 2 | 102 | 10 |
| 3 | 103 | NULL |
| 4 | 104 | 5 |
Practical Exercises
Exercise 1
Using the employees table, write a query to categorize employees based on their department and salary. If the department is 'IT' and the salary is greater than 60000, the category should be 'Senior Tech'. Otherwise, it should be 'Other'.
Solution
SELECT name,
department,
salary,
CASE
WHEN department = 'IT' AND salary > 60000 THEN 'Senior Tech'
ELSE 'Other'
END AS category
FROM employees;Exercise 2
Using the orders table, write a query to ensure that all null discounts are treated as zero and all zero discounts are treated as null.
Solution
Summary
In this section, we covered the following conditional expressions in SQL:
- CASE Statement: Allows for complex conditional logic.
- IF Statement: Provides a simple conditional check (specific to certain SQL dialects).
- COALESCE Function: Returns the first non-null value in a list.
- NULLIF Function: Returns null if two expressions are equal.
These tools are essential for writing flexible and powerful SQL queries. In the next section, we will explore subqueries and nested queries, which allow for even more complex data retrieval.
SQL Course
Module 1: Introduction to SQL
Module 2: Basic SQL Queries
Module 3: Working with Multiple Tables
Module 4: Advanced Data Filtering
- Using LIKE for Pattern Matching
- IN and BETWEEN Operators
- NULL Values and IS NULL
- Aggregating Data with GROUP BY
- HAVING Clause
Module 5: Data Manipulation
Module 6: Advanced SQL Functions
Module 7: Subqueries and Nested Queries
- Introduction to Subqueries
- Correlated Subqueries
- EXISTS and NOT EXISTS
- Using Subqueries in SELECT, FROM, and WHERE Clauses
Module 8: Indexes and Performance Optimization
- Understanding Indexes
- Creating and Managing Indexes
- Query Optimization Techniques
- Analyzing Query Performance
