- 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
- 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
- Multithreading and Multiprocessing in Python
- Context Managers in Python
- Decorators in Python
Random Blogs
- Top 10 Knowledge for Machine Learning & Data Science Students
- Ideas for Content of Every niche on Reader’s Demand during COVID-19
- Quantum AI – The Future of AI Powered by Quantum Computing
- Robotics & AI – How AI is Powering Modern Robotics
- The Ultimate Guide to Data Science: Everything You Need to Know
- Variable Assignment in Python
- The Ultimate Guide to Starting a Career in Computer Vision
- Store Data Into CSV File Using Python Tkinter GUI Library
- Create Virtual Host for Nginx on Ubuntu (For Yii2 Basic & Advanced Templates)
- AI in Cybersecurity: The Future of Digital Protection
- Avoiding the Beginner’s Trap: Key Python Fundamentals You Shouldn't Skip
- Mastering SQL in 2025: A Complete Roadmap for Beginners
- What Is SEO and Why Is It Important?
- Big Data: The Future of Data-Driven Decision Making
- Convert RBG Image to Gray Scale Image Using CV2
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