SQL query to find the number of distinct values in each column of a table

This technical problem deals with writing a SQL query to find the number of distinct values in each column of a table. The input is a table with two columns, and the output is the number of distinct values in each column.

Problem

Given a table with two columns, write a SQL query to find the number of distinct values in each column.
Input:
| Column1 | Column2 |
|---------|---------|
| 1       | 2       |
| 2       | 3       |
| 3       | 4       |
| 4       | 5       |
Output:
| Column1 | Column2 |
|---------|---------|
| 4       | 5       |

Solution

This solution is in SQL. Our A.I. can create solutions in multiple languages.
by sarastevens
SELECT COUNT(DISTINCT Column1) AS Column1, COUNT(DISTINCT Column2) AS Column2
FROM table;

/*

The solution above is optimal because it uses the COUNT function with the DISTINCT keyword. This will count the number of unique values in each column.

*/

A.I. Evaluation of the Solution

The solution above is optimal because it uses the COUNT function with the DISTINCT keyword. This will count the number of unique values in each column.

Evaluated at: 2023-01-29 02:15:29