How do you return unique city values from a table customers?

Study for the SQL Basics Test. Improve your knowledge with multiple choice questions and detailed explanations. Prepare effectively to master SQL concepts!

Multiple Choice

How do you return unique city values from a table customers?

Explanation:
To get unique city values, you remove duplicates from the results by using DISTINCT in the SELECT clause. The correct approach is: SELECT DISTINCT city FROM customers;. DISTINCT tells the database to return each city only once, no matter how many times a city appears in the table. If you omit DISTINCT, as in SELECT city FROM customers;, you’ll get every row’s city value, including duplicates. Placing DISTINCT after the column list, as in SELECT city DISTINCT FROM customers, isn’t valid SQL in most systems because DISTINCT belongs in the SELECT clause itself. Using GROUP BY city will produce one row per city, but that’s intended for aggregation and not just listing unique values.

To get unique city values, you remove duplicates from the results by using DISTINCT in the SELECT clause. The correct approach is: SELECT DISTINCT city FROM customers;. DISTINCT tells the database to return each city only once, no matter how many times a city appears in the table.

If you omit DISTINCT, as in SELECT city FROM customers;, you’ll get every row’s city value, including duplicates. Placing DISTINCT after the column list, as in SELECT city DISTINCT FROM customers, isn’t valid SQL in most systems because DISTINCT belongs in the SELECT clause itself. Using GROUP BY city will produce one row per city, but that’s intended for aggregation and not just listing unique values.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy