Home » SQL SELECT Statement

SQL SELECT Statement

SQL SELECT Statement

The SQL SELECT Statement is used to retrieve or fetch data from a database.

We can fetch either the entire table or according to some specified rules. The data returned is stored in a result table. This result table is also called the result set. With the SELECT clause of a SELECT command statement, we specify the columns that we want to be displayed in the query result and, optionally, which column headings we prefer to see above the result table.

The select clause is the first clause and is one of the last clauses of the select statement that the database server evaluates. The reason for this is that before we can determine what to include in the final result set, we need to know all of the possible columns that could be included in the final result set.

Syntax

SELECT * FROM TableName;
SQL

Examples of SELECT Statement in SQL

Example 1

CREATE TABLE Employees (
    ID INT PRIMARY KEY,
    Name VARCHAR(50),
    Department VARCHAR(50),
    Salary DECIMAL(10, 2)
);

INSERT INTO Employees (ID, Name, Department, Salary) VALUES
(1, 'John', 'IT', 50000.00),
(2, 'Jane', 'HR', 55000.00),
(3, 'Michael', 'Finance', 60000.00),
(4, 'Emily', 'Marketing', 52000.00);
SQL

This SQL code creates a table named “Employees” with columns for employee ID, name, department, and salary. It then inserts some sample data into the table.

Now, let’s use the SQL SELECT statement to retrieve data from the “Employees” table

SELECT * FROM Employees;
SQL

Output

| ID | Name    | Department | Salary   |
|----|---------|------------|----------|
| 1  | John    | IT         | 50000.00 |
| 2  | Jane    | HR         | 55000.00 |
| 3  | Michael | Finance    | 60000.00 |
| 4  | Emily   | Marketing  | 52000.00 |
SQL

Example 2

CREATE TABLE Student_Records   
(  
Student_Id Int PRIMARY KEY,    
First_Name VARCHAR (20),    
Address VARCHAR (20),    
Age Int NOT NULL,  
Percentage Int NOT NULL,  
Grade VARCHAR (10)   
) ;
INSERT INTO Student VALUES (201, Akash, Delhi, 18, 89, A2),   
(202, Bhavesh, Kanpur, 19, 93, A1),  
(203, Yash, Delhi, 20, 89, A2),    
(204, Bhavna, Delhi, 19, 78, B1),  
(05, Yatin, Lucknow, 20, 75, B1),  
(206, Ishika, Ghaziabad, 19, 51, C1),  
(207, Vivek, Goa, 20, 62, B2);  
SQL

Query

SELECT * FROM Student_Records;  
SQL

Output


Student_ID	First_Name	Address	  Age	Percentage	Grade
201	          Akash     	Delhi	   18	    89	     A2
202	         Bhavesh	    Kanpur	 19	     93	     A1
203	            Yash	     Delhi	  20	    89	   A2
204	           Bhavna	      Delhi	   19	    78	   B1
205	           Yatin	     Lucknow	  20	  75	   B1
206	          Ishika	    Ghaziabad	  19	   91	   C1
207	          Vivek	        Goa	      20	   80	   B2
SQL

Conclusion

In conclusion, the SQL SELECT statement is a powerful tool for retrieving data from one or more tables within a database. Its flexibility allows users to specify precisely which columns they want to retrieve and apply conditions to filter the result set based on specific criteria. Additionally, the SELECT statement can be combined with other SQL commands like JOIN, WHERE, GROUP BY, HAVING, and ORDER BY to further manipulate and refine the retrieved data.

Mastering the SELECT statement is essential for querying databases effectively and obtaining the information needed for various data analysis, reporting, and decision-making tasks. With its widespread use across different database management systems (DBMS) and its intuitive syntax, understanding how to use SELECT empowers users to interact with databases efficiently and extract valuable insights from their data.

Frequently Asked Questions

What is the SELECT statement used for?

The SQL SELECT statement is used to retrieve data from one or more tables in a database. It allows users to specify which columns they want to retrieve and can also include conditions to filter the data based on specific criteria.

What if I want to retrieve data from multiple tables?

You can use JOIN clauses to retrieve data from multiple tables in a single SELECT statement based on specified relationships between the tables.