Sorting and formatting data are super important skills that let you prep readable reports, make data analysis easier, and improve user experience. You'll need this stuff when building analytics reports, prepping data for export, and just in your day-to-day work with databases. In real life, you'll often run into tasks where you gotta make data look nice, remove duplicate records, and sort info so it's easy to read. That's exactly what we're gonna do today!
Example 1: Creating a List of Unique Customers with Combined First and Last Name, Sorted by Last Name
We've got a customers table that stores info about customers:
| id | first_name | last_name | city |
|---|---|---|---|
| 1 | Alex | Lin | New York |
| 2 | Maria | Chi | Los Angeles |
| 3 | Alex | Lin | New York |
| 4 | Anna | Song | Chicago |
Our goal:
- Combine
first_nameandlast_nameinto a single column calledfull_name. - Get only unique customers.
- Sort the list by last name (
last_name).
SQL query
SELECT DISTINCT
CONCAT(first_name, ' ', last_name) AS full_name,
city
FROM customers
ORDER BY full_name;
Important: with SELECT DISTINCT the ORDER BY can only use expressions from the SELECT list (or their aliases). The query ORDER BY last_name here would fail with for SELECT DISTINCT, ORDER BY expressions must appear in select list — that's why we sort by the already-combined full_name.
| full_name | city |
|---|---|
| Maria Chi | Los Angeles |
| Alex Lin | New York |
| Anna Song | Chicago |
Notice that the duplicate Alex Lin records were removed thanks to DISTINCT, and the whole list is sorted by last name in alphabetical order.
Example 2: Formatting Order Data and Sorting It
The orders table stores info about orders:
| order_id | customer_name | order_date | total_amount |
|---|---|---|---|
| 1 | Alex Lin | 2023-10-01 | 1500 |
| 2 | Maria Chi | 2023-10-02 | 2000 |
| 3 | Alex Lin | 2023-10-03 | 1500 |
| 4 | Anna Song | 2023-10-04 | 3000 |
Our goal:
- Create a
formatted_order_datecolumn where the order date is in DD-MM-YYYY format. - Remove duplicate customer and date records (keep only unique
customer_nameandorder_datecombos). - Sort orders by date in descending order.
- SQL query
SELECT DISTINCT
customer_name,
TO_CHAR(order_date, 'DD-MM-YYYY') AS formatted_order_date,
total_amount
FROM orders
ORDER BY formatted_order_date DESC;
Heads up: we sort by the alias formatted_order_date, not by order_date itself — otherwise with DISTINCT we'd get the error "ORDER BY expressions must appear in select list". Note that sorting goes by the DD-MM-YYYY string — lexicographically, not as an actual date.
Result:
| customer_name | formatted_order_date | total_amount |
|---|---|---|
| Anna Song | 04-10-2023 | 3000 |
| Alex Lin | 03-10-2023 | 1500 |
| Maria Chi | 02-10-2023 | 2000 |
See how with the TO_CHAR() function we changed the date format to DD-MM-YYYY, and with DISTINCT we got rid of duplicate records.
Example 3: Getting Unique "First Name + Last Name" Combos for Students and Sorting by Last Name and Birth Date
The students table has info about students:
| student_id | first_name | last_name | birth_date |
|---|---|---|---|
| 1 | Alex | Lin | 2001-03-15 |
| 2 | Maria | Chi | 2000-06-20 |
| 3 | Alex | Lin | 2001-03-15 |
| 4 | Anna | Song | 1999-10-10 |
Our goal:
- Combine first and last name into a single column
full_name. - Get unique "first name + last name" combos.
- Sort students by last name, then by birth date.
SELECT DISTINCT
CONCAT(first_name, ' ', last_name) AS full_name,
birth_date
FROM students
ORDER BY full_name, birth_date;
Result:
| full_name | birth_date |
|---|---|
| Maria Chi | 2000-06-20 |
| Alex Lin | 2001-03-15 |
| Anna Song | 1999-10-10 |
Heads up: two identical records for student "Alex Lin" were merged into one row, and sorting was done first by last name, then by birth date.
Practice Task
Use what you learned today to solve the following task:
Task: You have a products table with the following data:
| product_id | category | product_name | price |
|---|---|---|---|
| 1 | Electronics | Phone | 50000 |
| 2 | Clothing | Jacket | 8000 |
| 3 | Electronics | Laptop | 70000 |
| 4 | Clothing | Jacket | 8000 |
- Create a
formatted_productcolumn whereproduct_nameis combined with category using a dash, for example:Phone - Electronics. - Remove duplicate
product_nameandcategorycombos. - Sort products by category, then by price (from cheapest to most expensive).
Here's a query structure to help you out:
SELECT DISTINCT
CONCAT(product_name, ' - ', category) AS formatted_product,
price
FROM products
ORDER BY formatted_product, price ASC;
Try to imagine what the result of this query would look like!
Using CONCAT(), DISTINCT, and ORDER BY functions lets you get super readable and structured data, which is absolutely critical in real projects and tasks. Make sure you get how to combine them by practicing with examples!
GO TO FULL VERSION