SQLEasyFiltering10 min

Choose the first available contact

Return user ID and preferred contact: email first, then phone, then Unknown. Empty strings are present values and must not be replaced. Return only the requested columns and follow the specified ordering. This question probes coalesce selects the first non-null argument; unlike mysql ifnull, it is standard and supports multiple fallbacks.

Input

Setup script defining users(id INT, email VARCHAR(80), phone VARCHAR(30)).

Output

id, contact; ascending id.

Constraints

0–10000 rows per table. IDs are unique and non-NULL unless stated otherwise; foreign keys may be NULL. Numeric amounts are integers from 0 to 1000000. Dates are valid YYYY-MM-DD text. Text uses case-sensitive comparison; test data has consistent case. Duplicate business values and NULLs are allowed unless excluded by the task.

Example

Tables and sample rows

CREATE TABLE users(id INT, email VARCHAR(80), phone VARCHAR(30));
INSERT INTO users VALUES (1,'a@x','123'),(2,NULL,'456'),(3,NULL,NULL);

Expected result

id | contact
1 | a@x
2 | 456
3 | Unknown

Another dataset (1)

CREATE TABLE users(id INT, email VARCHAR(80), phone VARCHAR(30));
INSERT INTO users VALUES (1,'',NULL),(2,NULL,'');

Expected result

id | contact
1 | 
2 | 

Covers missing matches, ties, or NULL values without relying on insertion order.

Hints (2)
  1. List contact sources in priority order.
  2. Use COALESCE, not an empty-string test.

Target complexity: time O(n) · space O(1)

Your query

Checked instantly on your device with SQLite against 2 datasets. Rows must match the expected columns and order.

Reference solution

Sign in for solution
All SQL queries