- SQL Basics
-
Overview
- Introduction to Databases and SQL
- Creating and Managing Tables in SQL
- Data Types and Constraints
- SELECT Queries and Filtering Data
- GROUP BY, HAVING, and ORDER BY
- SQL Joins (INNER, LEFT, RIGHT, FULL)
- Subqueries and Nested Queries
- UNION, INTERSECT, and EXCEPT
- Common Table Expressions (CTE)
- SQL Views and Stored Procedures
SELECT Queries and Filtering Data
Add to BookmarkIntroduction
The SELECT
statement is the foundation of retrieving data from a database. It allows users to fetch specific columns, apply conditions, sort results, and perform aggregations. In this tutorial, we will cover different ways to use SELECT
queries, including filtering data using the WHERE
clause.
Basic SELECT
Statement
The simplest form of a SELECT
query retrieves all columns from a table:
SELECT * FROM employees;
This query returns all rows and columns from the employees
table.
Selecting Specific Columns
To retrieve only specific columns, list them in the SELECT
clause:
SELECT first_name, last_name, department FROM employees;
This query fetches only the first_name
, last_name
, and department
columns from the employees
table.
Filtering Data Using WHERE
The WHERE
clause is used to filter rows based on conditions:
SELECT * FROM employees WHERE department = 'IT';
This query retrieves all employees who belong to the IT department.
Using Comparison Operators
SQL provides several operators for filtering:
=
(equal to)!=
or<>
(not equal to)>
(greater than)<
(less than)>=
(greater than or equal to)<=
(less than or equal to)
Example:
SELECT * FROM employees WHERE salary > 50000;
This query retrieves employees with a salary greater than 50,000.
Using Logical Operators
Logical operators help in combining multiple conditions:
AND
: Both conditions must be trueOR
: At least one condition must be trueNOT
: Negates a condition
Example:
SELECT * FROM employees WHERE department = 'IT' AND salary > 50000;
This retrieves IT department employees earning more than 50,000.
Filtering with IN
, BETWEEN
, and LIKE
IN
: Matches any value in a given list
SELECT * FROM employees WHERE department IN ('IT', 'HR', 'Finance');
BETWEEN
: Selects values within a given range
SELECT * FROM employees WHERE salary BETWEEN 40000 AND 60000;
LIKE
: Searches for a pattern in text columns
SELECT * FROM employees WHERE first_name LIKE 'A%';
This retrieves employees whose first name starts with 'A'.
Sorting Results Using ORDER BY
To sort query results, use the ORDER BY
clause:
SELECT * FROM employees ORDER BY salary DESC;
This orders employees by salary in descending order.
To sort by multiple columns:
SELECT * FROM employees ORDER BY department ASC, salary DESC;
This sorts by department (ascending) and then by salary (descending).
Limiting Results Using LIMIT
The LIMIT
clause restricts the number of rows returned:
SELECT * FROM employees LIMIT 5;
This fetches only the first 5 rows.
For databases like SQL Server, use TOP
:
SELECT TOP 5 * FROM employees;
For Oracle, use FETCH FIRST
:
SELECT * FROM employees FETCH FIRST 5 ROWS ONLY;
Conclusion
The SELECT
statement is a powerful tool in SQL for retrieving and filtering data. By combining different clauses like WHERE
, ORDER BY
, LIMIT
, and logical operators, you can construct efficient queries to fetch exactly the data you need.
Prepare for Interview
- JavaScript Interview Questions for 0–1 Year Experience
- JavaScript Interview Questions For Fresher
- SQL Interview Questions for 5+ Years Experience
- SQL Interview Questions for 2–5 Years Experience
- SQL Interview Questions for 1–2 Years Experience
- SQL Interview Questions for 0–1 Year Experience
- SQL Interview Questions for Freshers
- Design Patterns in Python
- Dynamic Programming and Recursion in Python
- Trees and Graphs in Python
- Linked Lists, Stacks, and Queues in Python
- Sorting and Searching in Python
- Debugging in Python
- Unit Testing in Python
- Asynchronous Programming in PYthon
Random Blogs
- Mastering SQL in 2025: A Complete Roadmap for Beginners
- SQL Joins Explained: A Complete Guide with Examples
- How AI Companies Are Making Humans Fools and Exploiting Their Data
- Career Guide: Natural Language Processing (NLP)
- Important Mistakes to Avoid While Advertising on Facebook
- Google’s Core Update in May 2020: What You Need to Know
- Datasets for Exploratory Data Analysis for Beginners
- Understanding AI, ML, Data Science, and More: A Beginner's Guide to Choosing Your Career Path
- Datasets for Speech Recognition Analysis
- Where to Find Free Datasets for Your Next Machine Learning & Data Science Project
- Quantum AI – The Future of AI Powered by Quantum Computing
- What Is SEO and Why Is It Important?
- Types of Numbers in Python
- What to Do When Your MySQL Table Grows Too Wide
- Avoiding the Beginner’s Trap: Key Python Fundamentals You Shouldn't Skip
Datasets for Machine Learning
- Amazon Product Reviews Dataset
- Ozone Level Detection Dataset
- Bank Transaction Fraud Detection
- YouTube Trending Video Dataset (updated daily)
- Covid-19 Case Surveillance Public Use Dataset
- US Election 2020
- Forest Fires Dataset
- Mobile Robots Dataset
- Safety Helmet Detection
- All Space Missions from 1957
- OSIC Pulmonary Fibrosis Progression Dataset
- Wine Quality Dataset
- Google Audio Dataset
- Iris flower dataset
- Artificial Characters Dataset