SQL Challenge #11
Question
Consider the following students table:
students
Exercise
Write a SQL query to retrieve the names of students along with their class teachers. However, some students are not assigned a class teacher yet (i.e., ClassTeacher is NULL). In such cases, you need to display "Unassigned" instead of NULL.
The solution to this exercise can be found below - please take some time to work on the solution by yourself first before reading any further.
Solution
Query
Please note that the code below may not be visible if you are reading this article in your email. Kindly read the article on LinkedIn to access it.
SELECT StudentName,
COALESCE(ClassTeacher, 'Unassigned') AS ClassTeacher
FROM Students;
In this query:
领英推è
- We select the StudentName column from the Students table.
- We use the COALESCE function to handle NULL values. If the ClassTeacher column is NULL (meaning the student has no assigned class teacher, like Alice, Charlie and Emma), we display "Unassigned" instead.
Result
SQL Tips
- Understand COALESCE Function: COALESCE function in SQL is used to return the first non-null expression among its arguments. It's handy for dealing with NULL values effectively. The COALESCE() function is ANSI SQL standard.
- Similar functions: Other database systems might provide similar functionality with different functions. For example: In SQL Server, you can use the ISNULL() function and in Oracle, the NVL() function in order to fulfill the requirement.
- Replacing NULL Values: Use COALESCE to replace NULL values with a default or meaningful value in your result set. This is particularly useful when presenting data to users or performing calculations where NULLs might disrupt your operations..
- Use of alias: Ensure to consistently employ aliases when incorporating expressions within the SELECT statement, thereby enabling them to serve as column headers in the resulting output. In this question we've used "AS ClassTeacher" in the query.
- Avoiding Null-Induced Errors: Incorporating COALESCE can help prevent errors caused by NULL values in your calculations or comparisons. By replacing NULLs with appropriate defaults, you can ensure smoother query execution.
- Performance Consideration: While COALESCE is convenient, be mindful of its potential impact on query performance, especially when dealing with large datasets. Test and optimize your queries to ensure efficient execution.
- Combine with CASE Statements: In some cases, combining COALESCE with CASE statements can offer more flexibility in handling NULL values, especially when dealing with more complex conditional logic.
Feel free to comment your findings!
If you don't want to miss the carefully crafted SQL challenges every week, subscribe to my Newsletter. Elevate your skills through practice and unlock expert tips; be on top of your game!
Follow me for helpful posts about .NET/C#, SQL and software development.
Senior Software Developer @ Dayforce | Tech Enthusiast | Co-Founder of TechNews
1 å¹´Nice one. I learned something. I would usually use LINQ to do that ??