All SQL Query Commands
All SQL Query Commands
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;
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.
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
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.
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.
Example of LIKE clause
Consider the following Student table.
101 Adam 15
102 Alex 18
103 Abhi 17
The above query will return all records where s_name starts with character 'A'.
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.
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.
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,
The above query will return the resultant data in ascending order of the salary.
eid name age salary
The above query will return the resultant data in descending order of the salary.
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
name age
Rohan 34
Shane 29
Anu 22
FROM Emp
GROUP BY salary
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
GROUP BY column_name
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.
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
SELECT * FROM Emp WHERE salary < 10000 AND age > 25
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
The above query will return records where either salary is greater than 10000 or age is
greater than 25.