codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🗄️

SQL & Databases

14 / 67 topics
12Creating Tables13Inserting Data14Selecting Data15Updating Data16Deleting Data
Tutorials/SQL & Databases/Selecting Data
🗄️SQL & Databases

Selecting Data

Updated 2026-04-20
5 min read

Introduction

In this section, we will delve into one of the most fundamental operations in SQL: selecting data from a database. Understanding how to effectively select and retrieve data is crucial for any developer working with databases. We'll cover the basic syntax, advanced techniques, and best practices for querying data.

Basic SELECT Statement

The SELECT statement is used to query data from a database table. The simplest form of a SELECT statement retrieves all columns from a specified table.

Syntax

SELECT column1, column2, ...
FROM table_name;
  • column1, column2, ...: Specify the columns you want to retrieve. Use an asterisk (*) to select all columns.
  • table_name: The name of the table from which to retrieve data.

Example

SELECT * FROM Employees;

This query retrieves all columns for every row in the Employees table.

Filtering Data with WHERE Clause

The WHERE clause is used to filter records and return only those that meet specified conditions.

Syntax

SELECT column1, column2, ...
FROM table_name
WHERE condition;
  • condition: A logical expression that returns a boolean value (TRUE or FALSE). Only rows where the condition evaluates to TRUE are returned.

Example

SELECT FirstName, LastName
FROM Employees
WHERE Department = 'Sales';

This query retrieves the first name and last name of employees who work in the Sales department.

Sorting Data with ORDER BY Clause

The ORDER BY clause is used to sort the result set in ascending or descending order based on one or more columns.

Syntax

SELECT column1, column2, ...
FROM table_name
WHERE condition
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;
  • column1, column2, ...: The columns to sort by.
  • [ASC|DESC]: Optional. Specifies the sorting order. ASC (ascending) is the default.

Example

SELECT FirstName, LastName, Salary
FROM Employees
ORDER BY Salary DESC;

This query retrieves the first name, last name, and salary of all employees, sorted by salary in descending order.

Limiting Results with LIMIT Clause

The LIMIT clause restricts the number of rows returned by a SELECT statement.

Syntax

SELECT column1, column2, ...
FROM table_name
WHERE condition
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...
LIMIT number;
  • number: The maximum number of rows to return.

Example

SELECT FirstName, LastName
FROM Employees
ORDER BY Salary DESC
LIMIT 5;

This query retrieves the first name and last name of the top 5 highest-paid employees.

Using DISTINCT for Unique Values

The DISTINCT keyword is used to eliminate duplicate rows from the result set.

Syntax

SELECT DISTINCT column1, column2, ...
FROM table_name;

Example

SELECT DISTINCT Department
FROM Employees;

This query retrieves a list of unique departments in the company.

Advanced Filtering with AND/OR Operators

You can combine multiple conditions using AND and OR operators to refine your queries.

Syntax

SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 OR condition3;
  • AND: Returns rows where all conditions are true.
  • OR: Returns rows where at least one condition is true.

Example

SELECT FirstName, LastName
FROM Employees
WHERE Department = 'Sales' AND Salary > 50000;

This query retrieves the first name and last name of employees who work in Sales and earn more than $50,000.

Using IN Operator for Multiple Values

The IN operator allows you to specify multiple values in a WHERE clause.

Syntax

SELECT column1, column2, ...
FROM table_name
WHERE column_name IN (value1, value2, ...);

Example

SELECT FirstName, LastName
FROM Employees
WHERE Department IN ('Sales', 'Marketing');

This query retrieves the first name and last name of employees who work in either Sales or Marketing.

Using BETWEEN Operator for Ranges

The BETWEEN operator is used to select values within a given range.

Syntax

SELECT column1, column2, ...
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
  • value1: The lower bound of the range.
  • value2: The upper bound of the range.

Example

SELECT FirstName, LastName
FROM Employees
WHERE Salary BETWEEN 50000 AND 70000;

This query retrieves the first name and last name of employees whose salary is between $50,000 and $70,000.

Using LIKE Operator for Pattern Matching

The LIKE operator is used to search for a specified pattern in a column.

Syntax

SELECT column1, column2, ...
FROM table_name
WHERE column_name LIKE pattern;
  • pattern: A string that can include wildcard characters (% and _).

    • %: Matches any sequence of characters.
    • _: Matches a single character.

Example

SELECT FirstName, LastName
FROM Employees
WHERE LastName LIKE 'Sm%';

This query retrieves the first name and last name of employees whose last name starts with "Sm".

Using JOINs to Combine Data from Multiple Tables

JOINs are used to combine rows from two or more tables based on a related column between them.

Types of Joins

  1. INNER JOIN: Returns records that have matching values in both tables.
  2. LEFT JOIN (or LEFT OUTER JOIN): Returns all records from the left table, and the matched records from the right table. If there is no match, the result is NULL on the side of the right table.
  3. RIGHT JOIN (or RIGHT OUTER JOIN): Returns all records from the right table, and the matched records from the left table. If there is no match, the result is NULL on the side of the left table.
  4. FULL JOIN (or FULL OUTER JOIN): Returns all records when there is a match in either left or right table.

Syntax

SELECT column1, column2, ...
FROM table1
JOIN table2 ON table1.common_column = table2.common_column;

Example

SELECT Employees.FirstName, Employees.LastName, Departments.DepartmentName
FROM Employees
INNER JOIN Departments ON Employees.DepartmentID = Departments.ID;

This query retrieves the first name, last name, and department name of all employees by joining the Employees and Departments tables on the DepartmentID column.

Best Practices

  1. Specify Columns Explicitly: Instead of using SELECT *, specify only the columns you need to improve performance.
  2. Use WHERE Clause for Filtering: Always use a WHERE clause to filter data rather than retrieving all rows and filtering in your application code.
  3. Optimize Joins: Use appropriate join types based on your query requirements to avoid unnecessary data retrieval.
  4. Index Columns: Index columns that are frequently used in WHERE, JOIN, or ORDER BY clauses to speed up query performance.
  5. Limit Results: Use the LIMIT clause to restrict the number of rows returned, especially when dealing with large datasets.

Conclusion

In this section, we covered various aspects of selecting data in SQL, including basic syntax, filtering, sorting, limiting results, and combining data from multiple tables using JOINs. By mastering these techniques, you can write efficient and effective SQL queries to retrieve the data you need from your databases.


PreviousInserting DataNext Updating Data

Recommended Gear

Inserting DataUpdating Data