- 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 true
- OR: At least one condition must be true
- NOT: 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 5+ Years Experience
- JavaScript Interview Questions for 2–5 Years Experience
- JavaScript Interview Questions for 1–2 Years Experience
- 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
Random Blogs
- How AI Companies Are Making Humans Fools and Exploiting Their Data
- Understanding LLMs (Large Language Models): The Ultimate Guide for 2025
- Python Challenging Programming Exercises Part 2
- Loan Default Prediction Project Using Machine Learning
- Datasets for Speech Recognition Analysis
- Window Functions in SQL – The Ultimate Guide
- OLTP vs. OLAP Databases: Advanced Insights and Query Optimization Techniques
- Best Platform to Learn Digital Marketing in Free
- Navigating AI Careers in 2025: Data Science, Machine Learning, Deep Learning, and More
- Ideas for Content of Every niche on Reader’s Demand during COVID-19
- Top 10 Blogs of Digital Marketing you Must Follow
- Important Mistakes to Avoid While Advertising on Facebook
- What Is SEO and Why Is It Important?
- SQL Joins Explained: A Complete Guide with Examples
- Mastering SQL in 2025: A Complete Roadmap for Beginners
Datasets for Machine Learning
- Awesome-ChatGPT-Prompts
- 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

