Using LIMIT and DISTINCT in SQL
Introduction
When working with SQL for data analysis, handling large datasets efficiently is important. You often need to limit the number of results or remove duplicate values. The LIMIT and DISTINCT clauses help you refine your SQL queries and improve performance. In this lesson, you will learn how to use LIMIT and DISTINCT in SQL with practical examples.
What is LIMIT in SQL
The LIMIT clause is used to restrict the number of rows returned by a query. It is useful when you want to preview data or fetch only a specific number of records.
Basic syntax:
SELECT column_name FROM table_name LIMIT number;
Example:
SELECT * FROM employees LIMIT 5;
This query returns only the first 5 rows from the employees table.
Why Use LIMIT in Data Analysis
- Quickly preview large datasets
- Improve query performance
- Fetch top records for reporting
- Useful in dashboards and pagination
What is DISTINCT in SQL
The DISTINCT keyword is used to remove duplicate values from the result set. It ensures that only unique records are returned.
Basic syntax:
SELECT DISTINCT column_name FROM table_name;
Example:
SELECT DISTINCT department FROM employees;
This query returns unique department names without repetition.
Using LIMIT with ORDER BY
You can combine LIMIT with ORDER BY to get top or bottom results.
Example:
SELECT * FROM employees
ORDER BY salary DESC
LIMIT 3;
This query returns the top 3 highest-paid employees.
Using DISTINCT with Multiple Columns
DISTINCT can also be used with more than one column.
Example:
SELECT DISTINCT department, city FROM employees;
This returns unique combinations of department and city.
LIMIT vs DISTINCT
- LIMIT controls the number of rows returned
- DISTINCT removes duplicate records
- Both improve efficiency in data analysis
Why LIMIT and DISTINCT are Important
These clauses help you:
- Handle large datasets efficiently
- Avoid duplicate data in analysis
- Optimize SQL queries
- Extract clean and meaningful insights
Best Practices
- Use LIMIT when working with large tables
- Use DISTINCT only when necessary
- Combine with WHERE and ORDER BY for better results
- Test queries before running on full datasets
Summary
In this lesson, you learned how to use LIMIT to restrict results and DISTINCT to remove duplicates in SQL. These are essential tools for efficient SQL for data analysis.
FAQs
1. What is LIMIT in SQL?
LIMIT is used to restrict the number of rows returned by a query.
2. What does DISTINCT do in SQL?
DISTINCT removes duplicate values and returns unique records.
3. Can LIMIT and DISTINCT be used together?
Yes, they can be combined for better data filtering.
4. Does DISTINCT affect performance?
Yes, it can slow queries on large datasets, so use it carefully.
Internal Link
Want to explore more courses?
Click here for more free courses



