In SQL, the SELECT statement is used to query and retrieve data from a database. One of the most fundamental operations in SQL is selecting columns and rows from a table. The SELECT statement allows you to specify which columns you want to retrieve and filter the rows based on certain conditions.
The basic syntax of the SELECT statement involves specifying the columns you want to retrieve from a table. If you want to retrieve all columns, you can use the * wildcard.
SELECT column1, column2, ... FROM table_name;
-- Select the 'Name' and 'Age' columns from the 'Students' table SELECT Name, Age FROM Students;
In this example, the query retrieves the 'Name' and 'Age' columns from the 'Students' table. Only the specified columns are returned, and all rows are included unless filtered otherwise.
-- Select all columns from the 'Students' table SELECT * FROM Students;
In this example, the * wildcard is used to select all columns from the 'Students' table. This retrieves every column for every row in the table.
You can use the WHERE clause to filter rows and retrieve only those that meet specific conditions. The WHERE clause is a powerful tool for narrowing down the results of a SELECT statement.
SELECT column1, column2, ... FROM table_name WHERE condition;
-- Select all students who are older than 18 SELECT Name, Age FROM Students WHERE Age > 18;
This query retrieves the 'Name' and 'Age' of students who are older than 18. The WHERE clause ensures that only rows where the 'Age' is greater than 18 are returned.
-- Select students who are older than 18 and have a GPA greater than 3.5 SELECT Name, Age, GPA FROM Students WHERE Age > 18 AND GPA > 3.5;
In this example, both conditions must be true for a row to be included in the result. The query retrieves students who are older than 18 and have a GPA greater than 3.5.
SQL provides several logical operators that can be used in the WHERE clause to combine multiple conditions. The most commonly used logical operators are AND, OR, and NOT.
-- Select students who are either older than 18 or have a GPA greater than 3.5 SELECT Name, Age, GPA FROM Students WHERE Age > 18 OR GPA > 3.5;
In this case, the query retrieves students who meet either of the two conditions: being older than 18 or having a GPA greater than 3.5.
-- Select students who are not older than 18 SELECT Name, Age FROM Students WHERE NOT Age > 18;
This query returns students who are not older than 18 by using the NOT operator to negate the condition.
You can use the ORDER BY clause to sort the results of a SELECT statement. By default, the ORDER BY clause sorts the results in ascending order (from lowest to highest). You can use the DESC keyword to sort in descending order.
SELECT column1, column2, ... FROM table_name ORDER BY column1 [ASC|DESC];
-- Select all students and sort them by age in ascending order SELECT Name, Age FROM Students ORDER BY Age ASC;
This query retrieves all students and sorts them by the 'Age' column in ascending order (youngest to oldest).
-- Select all students and sort them by GPA in descending order SELECT Name, GPA FROM Students ORDER BY GPA DESC;
This query retrieves all students and sorts them by the 'GPA' column in descending order (highest to lowest).
In some cases, you may want to retrieve only a limited number of rows from the result set. SQL provides the LIMIT or TOP clause (depending on the database system) to restrict the number of rows returned.
-- Select the first 5 students from the 'Students' table SELECT Name, Age FROM Students LIMIT 5;
This query retrieves only the first 5 rows from the 'Students' table.
-- Select the top 5 students from the 'Students' table SELECT TOP 5 Name, Age FROM Students;
In SQL Server, the TOP keyword is used to limit the number of rows returned.
If you want to retrieve only unique (distinct) values from a column, you can use the DISTINCT keyword in your SELECT statement. This will remove duplicate values from the result set.
-- Select distinct ages from the 'Students' table SELECT DISTINCT Age FROM Students;
This query retrieves only the distinct ages from the 'Students' table, eliminating any duplicate values.
The SELECT statement is one of the most commonly used SQL commands for querying data. By understanding how to select specific columns, filter rows using conditions, sort results, limit the number of rows, and eliminate duplicates, you can effectively extract the data you need from your databases.