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.
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.
SELECT column1, column2, ...
FROM table_name;
*) to select all columns.SELECT * FROM Employees;
This query retrieves all columns for every row in the Employees table.
The WHERE clause is used to filter records and return only those that meet specified conditions.
SELECT column1, column2, ...
FROM table_name
WHERE condition;
TRUE or FALSE). Only rows where the condition evaluates to TRUE are returned.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.
The ORDER BY clause is used to sort the result set in ascending or descending order based on one or more columns.
SELECT column1, column2, ...
FROM table_name
WHERE condition
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;
ASC (ascending) is the default.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.
The LIMIT clause restricts the number of rows returned by a SELECT statement.
SELECT column1, column2, ...
FROM table_name
WHERE condition
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...
LIMIT number;
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.
The DISTINCT keyword is used to eliminate duplicate rows from the result set.
SELECT DISTINCT column1, column2, ...
FROM table_name;
SELECT DISTINCT Department
FROM Employees;
This query retrieves a list of unique departments in the company.
You can combine multiple conditions using AND and OR operators to refine your queries.
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 OR condition3;
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.
The IN operator allows you to specify multiple values in a WHERE clause.
SELECT column1, column2, ...
FROM table_name
WHERE column_name IN (value1, value2, ...);
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.
The BETWEEN operator is used to select values within a given range.
SELECT column1, column2, ...
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
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.
The LIKE operator is used to search for a specified pattern in a column.
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.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".
JOINs are used to combine rows from two or more tables based on a related column between them.
NULL on the side of the right table.NULL on the side of the left table.SELECT column1, column2, ...
FROM table1
JOIN table2 ON table1.common_column = table2.common_column;
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.
SELECT *, specify only the columns you need to improve performance.WHERE clause to filter data rather than retrieving all rows and filtering in your application code.WHERE, JOIN, or ORDER BY clauses to speed up query performance.LIMIT clause to restrict the number of rows returned, especially when dealing with large datasets.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.