Exploring the SQL SELECT Statement: Your Ultimate Guide
SQL SELECT Statement
The SQL SELECT statement is the cornerstone of querying in Structured Query Language (SQL). It enables developers to retrieve data from database tables and views with precision, efficiency, and flexibility. Whether you’re accessing specific records, filtering data with conditions, or sorting results, the SELECT statement serves as your go-to tool.
Complete Advance AI topics:-Â CLICK HERE
Complete Python Course with Advance topics:-Click here
What is the SQL SELECT Statement?
The SELECT statement is used to extract data from one or more tables in a database. It allows you to fetch specific columns or rows that meet defined criteria. The result of a SELECT query is stored in a result-set table.
Syntax of the SQL SELECT Statement
To retrieve specific columns:
SELECT Column_Name_1, Column_Name_2, ..., Column_Name_N
FROM Table_Name;
To retrieve all columns from a table:
SELECT *
FROM Table_Name;
Examples of the SELECT Statement
Example 1: Basic SELECT Query
Create a Student_Records
table and insert data:
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_Records 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');
Retrieve all data:
SELECT * FROM Student_Records;
Output:
Student_ID | First_Name | Address | Age | Percentage | Grade |
---|---|---|---|---|---|
201 | Akash | Delhi | 18 | 89 | A2 |
202 | Bhavesh | Kanpur | 19 | 93 | A1 |
Example 2: SELECT with Specific Columns
SELECT Student_Id, Age, Percentage
FROM Student_Records;
Output:
Student_ID | Age | Percentage |
---|---|---|
201 | 18 | 89 |
202 | 19 | 93 |
Using the WHERE Clause
The WHERE clause filters rows based on specific conditions.
Example: Filtering Data
Create the Employee_Details
table:
CREATE TABLE Employee_Details (
Employee_ID INT PRIMARY KEY,
Emp_Name VARCHAR(50),
Emp_City VARCHAR(20),
Emp_Salary INT NOT NULL,
Emp_Penalty INT NOT NULL
);
INSERT INTO Employee_Details VALUES
(101, 'Anuj', 'Ghaziabad', 25000, 500),
(102, 'Tushar', 'Lucknow', 29000, 1000),
(103, 'Vivek', 'Kolkata', 35000, 500);
Filter employees with a penalty of 500:
SELECT *
FROM Employee_Details
WHERE Emp_Penalty = 500;
Output:
Employee_ID | Emp_Name | Emp_City | Emp_Salary | Emp_Penalty |
---|---|---|---|---|
101 | Anuj | Ghaziabad | 25000 | 500 |
103 | Vivek | Kolkata | 35000 | 500 |
GROUP BY Clause
The GROUP BY clause groups rows with similar values.
Example: Aggregating Data
Create the Cars_Details
table:
CREATE TABLE Cars_Details (
Car_Number INT PRIMARY KEY,
Car_Name VARCHAR(50),
Car_Price INT NOT NULL,
Car_Amount INT NOT NULL
);
INSERT INTO Cars_Details VALUES
(2578, 'Creta', 1500000, 3),
(9258, 'Audi', 3000000, 2),
(8233, 'Venue', 900000, 6),
(6214, 'Nexon', 1000000, 7);
Count cars by price:
SELECT COUNT(Car_Name), Car_Price
FROM Cars_Details
GROUP BY Car_Price;
Output:
Count(Car_Name) | Car_Price |
---|---|
2 | 1000000 |
2 | 900000 |
HAVING Clause
Use the HAVING clause to filter groups defined by the GROUP BY clause.
Example: Filtering Groups
SELECT SUM(Car_Price), Car_Name
FROM Cars_Details
GROUP BY Car_Name
HAVING SUM(Car_Price) > 1000000;
ORDER BY Clause
The ORDER BY clause sorts the result in ascending or descending order.
Example: Sorting Data
Create the Employee_Order
table:
CREATE TABLE Employee_Order (
Id INT PRIMARY KEY,
FirstName VARCHAR(50),
Salary INT,
City VARCHAR(50)
);
INSERT INTO Employee_Order VALUES
(201, 'Jone', 20000, 'Goa'),
(202, 'Basant', 15000, 'Delhi'),
(203, 'Rashet', 80000, 'Jaipur'),
(204, 'Anuj', 90000, 'Goa');
Sort by salary (descending):
SELECT *
FROM Employee_Order
ORDER BY Salary DESC;
Output:
Id | FirstName | Salary | City |
---|---|---|---|
204 | Anuj | 90000 | Goa |
203 | Rashet | 80000 | Jaipur |
Download New Real Time Projects :-Click here
sql select statement examples
sql query examples
sql query to retrieve data from database
sql query examples with answers
a subquery in an sql select statement
update query in sql
insert query in sql
sql commands
online sql compiler
w3schools
sql select statement example
sql select statement w3schools
sql select statement oracle
Post Comment