8. Use COALESCE for Null Handling
Beginner Mode
Objective
Given a table named orders with the columns order_id, customer_name, discount, and total_amount, write an SQL query to retrieve all orders. Replace any NULL values in the discount column with 0. The result should be ordered by order_id in ascending order.
Additional information
- The
orderstable schema:
| Column | Type | Description |
|---|---|---|
| order_id | Integer | Unique identifier for each order |
| customer_name | String | Name of the customer who placed the order |
| discount | Decimal | Discount amount applied to the order, can be NULL |
| total_amount | Decimal | Total amount of the order |
- Ensure that the
discountcolumn in the result does not contain anyNULLvalues; use0instead where applicable. - The output should include all columns:
order_id,customer_name,discount, andtotal_amount, in that order. - Order the results by
order_idin ascending order.
Examples
Example 1:
Output:
Input:
| orders | |||
|---|---|---|---|
| customer_name | discount | order_id | total_amount |
| John Smith | null | 1 | 150 |
| Alice Brown | 25 | 2 | 300 |
| Mike Wilson | null | 3 | 75.5 |
| Sarah Davis | 15 | 4 | 200 |
| customer_name | discount | order_id | total_amount |
|---|---|---|---|
| John Smith | 0 | 1 | 150 |
| Alice Brown | 25 | 2 | 300 |
| Mike Wilson | 0 | 3 | 75.5 |
| Sarah Davis | 15 | 4 | 200 |
Code Environment
Sign in or try as guest to run your code.
Essential
SQL 0/33
Git 0/15
Spark 0/20
Snowflake 0/22
Python 0/24
Need more practice in this area? Explore more questions →
Samsung
TCS
X
Accenture
Adobe
Google
LinkedIn
Datadog
Wix
Dropbox
Meta
OpenAI
Hulu
Uber
DoorDash
Anthropic
Amazon
ActivisionBlizzard
Vercel
Crypto.Com
Zscaler
DeutscheBank
Apple
GoDaddy
BMW
PayPal
Snowflake
AMD
Twilio
Atlassian
JPMorgan
NVIDIA
IBM
Databricks
Coinbase
Cisco
Robinhood
Twitter
Microsoft
Palantir
Netflix
VMware
Cloudflare
Stripe
Capital One
Splunk
Intel
SAP
Tesla
GitHub
JaneStreet
Bloomberg
Salesforce
Elastic
CGI
UBS
GitLab
Ubisoft
Slack
Nintendo
EY
Kayak
Lyft
Airbnb
Walmart
Revolut
Visa
Okta
HashiCorp
Instacart
Mastercard