SELECT
SQL Query
SELECT query is used to retrieve data from a table. It is the most used SQL query. We
can retrieve complete table data, or partial by specifying conditions using
the WHERE clause.
Syntax of SELECT query
SELECT query is used to retieve records from a table. We can specify the names of the
columns which we want in the resultset.
SELECT
column_name1,
column_name2,
column_name3,
...
column_nameN
FROM table_name;
Time for an Example
Consider the following student table,
s_id name age address
101 Adam 15 Chennai
102 Alex 18 Delhi
103 Abhi 17 Banglore
104 Ankit 22 Mumbai
SELECT s_id, name, age FROM student;
The above query will fetch information of s_id, name and age columns of
the student table and display them,
s_id name age
101 Adam 15
102 Alex 18
103 Abhi 17
104 Ankit 22
As you can see the data from address column is absent, because we did not specif it in
our SELECT query.
Select all records from a table
A special character asterisk * is used to address all the data(belonging to all columns)
in a query. SELECT statement uses * character to retrieve all records from a table, for all
the columns.
SELECT * FROM student;
The above query will show all the records of student table, that means it will show
complete dataset of the table.
s_id name age address
101 Adam 15 Chennai
102 Alex 18 Delhi
103 Abhi 17 Banglore
104 Ankit 22 Mumbai
Select a particular record based on a condition
We can use the WHERE clause to set a condition,
SELECT * FROM student WHERE name = 'Abhi';
The above query will return the following result,
103 Abhi 17 Rohtak
Performing Simple Calculations using SELECT Query
Consider the following employee table.
eid name age salary
101 Adam 26 5000
102 Ricky 42 8000
103 Abhi 25 10000
104 Rohan 22 5000
Here is our SELECT query,
SELECT eid, name, salary+3000 FROM employee;
The above command will display a new column in the result, with 3000 added into
existing salaries of the employees.
eid name salary+3000
101 Adam 8000
102 Ricky 11000
103 Abhi 13000
104 Rohan 8000
So you can also perform simple mathematical operations on the data too using
the SELECT query to fetch data from table.
SQL LIKE clause
LIKE clause is used in the condition in SQL query with the WHERE clause. LIKE clause
compares data with an expression using wildcard operators to match pattern given in
the condition.
Wildcard operators
There are two wildcard operators that are used in LIKE clause.
Percent sign %: represents zero, one or more than one character.
Underscore sign _: represents only a single character.
Example of LIKE clause
Consider the following Student table.
s_id s_Name age
101 Adam 15
102 Alex 18
103 Abhi 17
SELECT * FROM Student WHERE s_name LIKE 'A%';
The above query will return all records where s_name starts with character 'A'.
s_id s_Name age
101 Adam 15
102 Alex 18
103 Abhi 17
Using _ and %
SELECT * FROM Student WHERE s_name LIKE '_d%';
The above query will return all records from Student table where s_name contain 'd' as
second character.
s_id s_Name age
101 Adam 15
Using % only
SELECT * FROM Student WHERE s_name LIKE '%x';
The above query will return all records from Student table where s_name contain 'x' as
last character.
s_id s_Name age
102 Alex 18
SQL ORDER BY Clause
Order by clause is used with SELECT statement for arranging retrieved data in sorted
order. The Order by clause by default sorts the retrieved data in ascending order. To
sort the data in descending order DESC keyword is used with Order by clause.
Syntax of Order By
SELECT column-list|* FROM table-name ORDER BY ASC | DESC;
Using default Order by
Consider the following Emp table,
eid name age salary
401 Anu 22 9000
402 Shane 29 8000
403 Rohan 34 6000
404 Scott 44 10000
405 Tiger 35 8000
SELECT * FROM Emp ORDER BY salary;
The above query will return the resultant data in ascending order of the salary.
eid name age salary
403 Rohan 34 6000
402 Shane 29 8000
405 Tiger 35 8000
401 Anu 22 9000
404 Scott 44 10000
Using Order by DESC
Consider the Emp table described above,
SELECT * FROM Emp ORDER BY salary DESC;
The above query will return the resultant data in descending order of the salary.
eid name age salary
404 Scott 44 10000
401 Anu 22 9000
405 Tiger 35 8000
402 Shane 29 8000
403 Rohan 34 6000
SQL Group By Clause
Group by clause is used to group the results of a SELECT query based on one or more
columns. It is also used with SQL functions to group the result from one or more tables.
Syntax for using Group by in a statement.
SELECT column_name, function(column_name)
FROM table_name
WHERE condition
GROUP BY column_name
Example of Group by in a Statement
Consider the following Emp table.
eid name age salary
401 Anu 22 9000
402 Shane 29 8000
403 Rohan 34 6000
404 Scott 44 9000
405 Tiger 35 8000
Here we want to find name and age of employees grouped by their salaries or in other
words, we will be grouping employees based on their salaries, hence, as a result, we
will get a data set, with unique salaries listed, along side the first employee's name and
age to have that salary. Hope you are getting the point here!
group by is used to group different row of data together based on any one column.
SQL query for the above requirement will be,
SELECT name, age
FROM Emp GROUP BY salary
Result will be,
name age
Rohan 34
Shane 29
Anu 22
Example of Group by in a Statement with WHERE clause
Consider the following Emp table
eid name age salary
401 Anu 22 9000
402 Shane 29 8000
403 Rohan 34 6000
404 Scott 44 9000
405 Tiger 35 8000
SQL query will be,
SELECT name, salary
FROM Emp
WHERE age > 25
GROUP BY salary
Result will be.
name salary
Rohan 6000
Shane 8000
Scott 9000
You must remember that Group By clause will always come at the end of the SQL
query, just like the Order by clause.
SQL HAVING Clause
Having clause is used with SQL Queries to give more precise condition for a statement.
It is used to mention condition in Group by based SQL queries, just like WHERE clause is
used with SELECT query.
Syntax for HAVING clause is,
SELECT column_name, function(column_name)
FROM table_name
WHERE column_name condition
GROUP BY column_name
HAVING function(column_name) condition
Example of SQL Statement using HAVING
Consider the following Sale table.
oid order_name previous_balance customer
11 ord1 2000 Alex
12 ord2 1000 Adam
13 ord3 2000 Abhi
14 ord4 1000 Adam
15 ord5 2000 Alex
Suppose we want to find the customer whose previous_balance sum is more
than 3000.
We will use the below SQL query,
SELECT *
FROM sale GROUP BY customer
HAVING sum(previous_balance) > 3000
Result will be,
oid order_name previous_balance customer
11 ord1 2000 Alex
The main objective of the above SQL query was to find out the name of the customer
who has had a previous_balance more than 3000, based on all the previous sales
made to the customer, hence we get the first row in the table for customer Alex.
DISTINCT keyword
The distinct keyword is used with SELECT statement to retrieve unique values from
the table. Distinct removes all the duplicate records while retrieving records from any
table in the database.
Syntax for DISTINCT Keyword
SELECT DISTINCT column-name FROM table-name;
Example using DISTINCT Keyword
Consider the following Emp table. As you can see in the table below, there is
employee name, along with employee salary and age.
In the table below, multiple employees have the same salary, so we will be
using DISTINCT keyword to list down distinct salary amount, that is currently being paid
to the employees.
eid name age salary
401 Anu 22 5000
402 Shane 29 8000
403 Rohan 34 10000
404 Scott 44 10000
405 Tiger 35 8000
SELECT DISTINCT salary FROM Emp;
The above query will return only the unique salary from Emp table.
salary
5000
8000
10000
SQL AND & OR operator
The AND and OR operators are used with the WHERE clause to make more precise
conditions for fetching data from database by combining more than one condition
together.
AND operator
AND operator is used to set multiple conditions with the WHERE clause,
alongside, SELECT, UPDATE or DELETE SQL queries.
Example of AND operator
Consider the following Emp table
eid name age salary
401 Anu 22 5000
402 Shane 29 8000
403 Rohan 34 12000
404 Scott 44 10000
405 Tiger 35 9000
SELECT * FROM Emp WHERE salary < 10000 AND age > 25
The above query will return records where salary is less than 10000 and age greater
than 25. Hope you get the concept here. We have used the AND operator to specify two
conditions with WHERE clause.
eid name age salary
402 Shane 29 8000
405 Tiger 35 9000
OR operator
OR operator is also used to combine multiple conditions with WHERE clause. The only
difference between AND and OR is their behaviour.
When we use AND to combine two or more than two conditions, records satisfying all the
specified conditions will be there in the result.
But in case of OR operator, atleast one condition from the conditions specified must be
satisfied by any record to be in the resultset.
Example of OR operator
Consider the following Emp table
eid name age salary
401 Anu 22 5000
402 Shane 29 8000
403 Rohan 34 12000
404 Scott 44 10000
405 Tiger 35 9000
SELECT * FROM Emp WHERE salary > 10000 OR age > 25
The above query will return records where either salary is greater than 10000 or age is
greater than 25.
402 Shane 29 8000
403 Rohan 34 12000
404 Scott 44 10000
405 Tiger 35 9000