DISTINCT and Aliases
SQL gives you tools to clean up your results and rename columns to make your output easier to read and work with.
The DISTINCT
keyword removes duplicate values, and the AS
keyword gives aliases to columns or tables.
DISTINCT: Remove Duplicate Values
The DISTINCT
keyword is used in the SELECT
clause to return only unique values.
Remove duplicate book titles
SELECT DISTINCT title
FROM book_checkouts;
If the same book has been checked out multiple times, this will show each title only once.
AS: Rename Columns and Tables
The AS
keyword allows you to assign an alias, a temporary name, to a column or table in your query.
Rename columns for clarity
SELECT title AS book_title, member_id AS borrower
FROM book_checkouts;
In the result, column headers will appear as book_title
and borrower
instead of title
and member_id
.
Why distinct and aliases matter
These tools help you:
- Eliminate unnecessary repetition
- Clean up the structure of your results
- Make reports and queries more readable
Aliases are especially useful when working with:
- Long or complex column names
- Joining multiple tables with overlapping names
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.