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

Conteúdo do Curso

SQL Basics

SQL Basics

1. Selecting
2. Filtering
3. Aggregating
4. Sorting
5. Grouping
6. Practicing

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,

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.

Tarefa

Extract all unique combinations of model and transmission

Tarefa

Extract all unique combinations of model and transmission

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 1. Capítulo 5
toggle bottom row

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,

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.

Tarefa

Extract all unique combinations of model and transmission

Tarefa

Extract all unique combinations of model and transmission

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 1. Capítulo 5
toggle bottom row

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,

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.

Tarefa

Extract all unique combinations of model and transmission

Tarefa

Extract all unique combinations of model and transmission

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

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,

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.

Tarefa

Extract all unique combinations of model and transmission

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 1. Capítulo 5
Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt