Contents
SQL SELECT, DISTINCT and Aliases Explained
Every query you write for analysis starts with SELECT. It says which columns you want, and FROM says which table they live in. Think of it as asking a question of a spreadsheet: which columns, from which sheet.
Here is a small movies table we will query.
movie_id | title | genre | release_year | rating |
| 1 | Silent Harbor | Drama | 2018 | 7.9 |
| 2 | Red Monsoon | Thriller | 2021 | 8.1 |
| 3 | Paper Kites | Drama | 2019 | 7.4 |
| 4 | The Last Signal | Sci-Fi | 2022 | 8.4 |
| 5 | Neon Streets | Thriller | 2020 | 7.7 |
| 6 | Quiet Orbit | Sci-Fi | 2023 | 8.0 |
Picking Columns and Renaming Them
SELECT * returns every column, which is handy for a quick look. For real work, name the columns you need. The query runs faster and the result is easier to read.
SELECT title AS movie, rating
FROM movies;Output:
movie | rating |
| Silent Harbor | 7.9 |
| Red Monsoon | 8.1 |
| Paper Kites | 7.4 |
| The Last Signal | 8.4 |
| Neon Streets | 7.7 |
| Quiet Orbit | 8.0 |
Code Explanation:
- SELECT lists the two columns we want, and the other columns are left out.
- AS movie is an alias. It renames the title column in the result set only; the table itself is not changed.
- FROM movies tells the database where to read from.
Aliases are not limited to column names. They also label calculated values, such as rating * 10 AS score_out_of_100.
Removing Repeats with DISTINCT
Sometimes you want to know what values exist in a column, not how often they appear. DISTINCT returns each value once.
SELECT DISTINCT genre
FROM movies;Output:
| genre |
| Drama |
| Thriller |
| Sci-Fi |
Code Explanation:
- The table has six rows, but only three different genres.
- DISTINCT collapses the repeats into one row per value.
- The order of the rows is not guaranteed unless you sort them.
SQL for Data Science
Abhishek Pati








