
- SQL - Home
- SQL - Roadmap
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL - Comments
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Cheatsheet
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
SQL SELECT Statement
SQL SELECT Statement
The SELECT statement in SQL is used to retrieve data from one or more tables in a database. It is one of the most commonly used commands, allowing you to view the records stored in your tables.
With the SELECT statement, you can retrieve all columns or choose specific ones, depending on what you need. You can also apply conditions to filter the results, sort the data, group similar records, or limit how many rows are returned. It gives you full control over how data is displayed from the table.
Syntax
Following is the basic syntax of the SELECT statement to retrieve all columns from a table:
SELECT * FROM table_name;
Example
Let us assume we have created a table named CUSTOMERS in the MySQL database:
CREATE TABLE CUSTOMERS ( ID INT NOT NULL, NAME VARCHAR(20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(25), SALARY DECIMAL(18, 2), PRIMARY KEY (ID) );
Now, insert values into this table using the INSERT statement as follows:
INSERT INTO CUSTOMERS VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 ), (2, 'Khilan', 25, 'Delhi', 1500.00 ), (3, 'Kaushik', 23, 'Kota', 2000.00 ), (4, 'Chaitali', 25, 'Mumbai', 6500.00 ), (5, 'Hardik', 27, 'Bhopal', 8500.00 ), (6, 'Komal', 22, 'Hyderabad', 4500.00 ), (7, 'Muffy', 24, 'Indore', 10000.00 );
Now, to select all the records from the CUSTOMERS table, use the following SQL statement:
SELECT * FROM CUSTOMERS;
The query will display all rows and columns from the table as shown below:
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Ramesh | 32 | Ahmedabad | 2000.00 |
2 | Khilan | 25 | Delhi | 1500.00 |
3 | Kaushik | 23 | Kota | 2000.00 |
4 | Chaitali | 25 | Mumbai | 6500.00 |
5 | Hardik | 27 | Bhopal | 8500.00 |
6 | Komal | 22 | Hyderabad | 4500.00 |
7 | Muffy | 24 | Indore | 10000.00 |
Select Specific Columns
If you don't want all columns, you can retrieve specific columns by mentioning their names in the SELECT statement.
Syntax
Following is the basic syntax to select specific columns from a table:
SELECT column1, column2, ... FROM table_name;
Example
Following is an example to get only the ID and NAME of each customer in the CUSTOMERS table:
SELECT ID, NAME FROM CUSTOMERS;
We get the following table as an output:
ID | NAME |
---|---|
1 | Ramesh |
2 | Khilan |
3 | Kaushik |
4 | Chaitali |
5 | Hardik |
6 | Komal |
7 | Muffy |
Using SELECT with WHERE Clause
You can use the SELECT statement with the WHERE clause to filter records based on a condition. This allows you to retrieve only the rows that match your criteria.
Syntax
Following is the syntax use the SELECT statement with the WHERE clause:
SELECT * FROM table_name WHERE condition;
Example
In this example, we get retrieve the data of all customers whose AGE is greater than 25:
SELECT * FROM CUSTOMERS WHERE AGE > 25;
We get the output as shown below:
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Ramesh | 32 | Ahmedabad | 2000.00 |
5 | Hardik | 27 | Bhopal | 8500.00 |
Using SELECT with ORDER BY Clause
You can use the SELECT statement with the ORDER BY clause to sort the results based on one or more columns. By default, the sorting is done in ascending order. If you want to sort the data in descending order, you can add the DESC keyword.
Syntax
Following is the syntax to use the SELECT statement with the ORDER BY clause:
SELECT * FROM table_name ORDER BY column1 [ASC|DESC];
Example
In this example, we list all customers sorted by AGE in descending order:
SELECT * FROM CUSTOMERS ORDER BY AGE DESC;
Following is the output obtained:
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Ramesh | 32 | Ahmedabad | 2000.00 |
5 | Hardik | 27 | Bhopal | 8500.00 |
2 | Khilan | 25 | Delhi | 1500.00 |
4 | Chaitali | 25 | Mumbai | 6500.00 |
7 | Muffy | 24 | Indore | 10000.00 |
3 | Kaushik | 23 | Kota | 2000.00 |
6 | Komal | 22 | Hyderabad | 4500.00 |
Using SELECT with LIMIT Clause
You can use the SELECT statement with the LIMIT clause to limit the number of rows returned in the result set. This is useful when you only want to see a specific number of records, especially in large tables. For example, you can limit the results to just the first 5 or 10 rows.
Syntax
Following is the syntax to use the SELECT statement with the LIMIT clause:
SELECT * FROM table_name LIMIT number;
Example
In the following example, we retrieve the first 3 customers from the table:
SELECT * FROM CUSTOMERS LIMIT 3;
We get the output as shown below:
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Ramesh | 32 | Ahmedabad | 2000.00 |
2 | Khilan | 25 | Delhi | 1500.00 |
3 | Kaushik | 23 | Kota | 2000.00 |
Using Aliases with SELECT
You can use aliases with the SELECT statement to give temporary names to columns or tables in your query. Aliases are created using the AS keyword, but it is optional i.e. you can also use a space.
Aliases are helpful when column names are long or when you are using functions or combining multiple tables.
Syntax
Following is the syntax to use aliases with the SELECT statement:
SELECT column_name AS alias_name FROM table_name;
Example
In the following example, we use an alias to show the customer names as "CustomerName":
SELECT NAME AS CustomerName FROM CUSTOMERS;
We get the following output:
CustomerName |
---|
Ramesh |
Khilan |
Kaushik |
Chaitali |
Hardik |
Komal |
Muffy |
Important Points About SELECT Statement
When using the SELECT statement, you need to keep the following points in mind:
- Use * to select all columns, or list specific column names to retrieve only the data you need.
- Use the WHERE clause to filter records, and ORDER BY to sort the results.
- The LIMIT clause is useful for restricting the number of rows returned, especially helpful for pagination or previewing data.
- Use AS to create aliases for columns, making the output easier to read and understand.