Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Unique values from multiple columns | Selecting
SQL Basics

book
Unique values from multiple columns

But what if we want to know, for example, unique pairs 'country-city' of our site visitors? Surely, we can extract unique countries (like in the previous chapter) and unique cities. But then we have to connect each city to the country manually.

This task can be solved by the same DISTINCT, but this time with two columns. For example,

SELECT DISTINCT(country), city
FROM visitors
12
SELECT DISTINCT(country), city FROM visitors
copy

The DISTINCT statement will return all the unique combinations of columns you chose. Using parentheses is not mandatory, i.e. you for the example above you can write DISTINCT country, city. But placing multiple columns within DISTINCT parentheses will return only one column of all unique combinations in (country, city) format.

Tarea

Swipe to start coding

Extract all unique combinations of model and transmission

Solución

SELECT DISTINCT model, transmission
FROM audi_cars

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 5

Query ResultQuery Result
No query executed yet...

Pregunte a AI

expand
ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

We use cookies to make your experience better!
some-alt