Open In App
Related Articles

SQL | SELECT Query

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The SELECT Statement in SQL 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.

CREATE TABLE:

CREATE TABLE Customer(
    CustomerID INT PRIMARY KEY,
    CustomerName VARCHAR(50),
    LastName VARCHAR(50),
    Country VARCHAR(50),
    Age int(2),
  Phone int(10)
);
-- Insert some sample data into the Customers table
INSERT INTO Customer (CustomerID, CustomerName, LastName, Country, Age, Phone)
VALUES (1, 'Shubham', 'Thakur', 'India','23','xxxxxxxxxx'),
       (2, 'Aman ', 'Chopra', 'Australia','21','xxxxxxxxxx'),
       (3, 'Naveen', 'Tulasi', 'Sri lanka','24','xxxxxxxxxx'),
       (4, 'Aditya', 'Arpan', 'Austria','21','xxxxxxxxxx'),
       (5, 'Nishant. Salchichas S.A.', 'Jain', 'Spain','22','xxxxxxxxxx');

Output:

 

To fetch any column in the table.

Syntax:

SELECT column1,column2 FROM table_name 

column1 , column2: names of the fields of the table

table_name: from where we want to apply query

This query will return all the rows in the table with fields column1 and column2.

SELECT Statement in SQL

To fetch the entire table or all the fields in the table:

Syntax:

 SELECT * FROM table_name; 

 — asterisks represent all attributes of the table

Query to fetch the fields CustomerName, LastName from the table Customer:

SELECT CustomerName, LastName FROM Customer;

Output:

img2

 

To fetch all the fields from the table Customer:

 SELECT * FROM Customer;

Output:

 

SELECT Statement with WHERE Clause

Suppose we want to see table values with specific conditions then Where Clause is used with select statement. 

Query:

SELECT CustomerName FROM Customer where Age = '21'; 

Output:

 

SQL SELECT Statement with GROUP BY Clause

Query:

SELECT COUNT (item), Customer_id FROM Orders GROUP BY order_id;  

Output:

 

SELECT Statement with HAVING Clause

Consider the following database for Having Clause:

 

Query:

SELECT Department, sum(Salary) as Salary
FROM employee
GROUP BY department
HAVING SUM(Salary) >= 50000;  

Output:

 

SELECT Statement with ORDER BY clause in SQL

Query:

SELECT * FROM Customer ORDER BY Age DESC;  

Output:

 

 


Last Updated : 07 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads