Chapter 9
Chapter 9
SQL Concepts
Prepared By:
Ms. Ayushi Gondaliya
Assistant Prof.
SRCOE, RAJKOT
What Is SQL?
SQL stands for Structured Query Language.
It is used for storing and managing data in
relational database management system (RDMS).
It is a standard language for Relational Database
System. It enables a user to create, read, update
and delete relational databases and tables
All the RDBMS like MySQL, Informix, Oracle, MS
Access and SQL Server use SQL as their standard
database language.
SQL allows users to query the database in a
number of ways, using English-like statements.
Rules:
SQL follows the following rules:
1. Structure query language is not case
sensitive. Generally, keywords of SQL are
written in uppercase.
2. Statements of SQL are dependent on text
lines. We can use a single SQL statement
on one or multiple text line.
3. Using the SQL statements, you can
perform most of the actions in a database.
4. SQL depends on tuple relational calculus
and relational algebra.
SQL Data types:
SQL Data type is used to define the values that
a column can contain.
Every column is required to have a name and
data type in the database table.
Data type of SQL:
1. Numeric data type
2. Exact numeric data type
3. String data type
4. Date data type
1. Numeric Data type:
The subtypes of approximate numeric
data types are given below:
Data type From To Description
Item1 Com1 2 10 20
Item2 Com2 3 25 75
Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40
Item6 Cpm1 3 25 75
Item7 Com1 5 30 150
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Item10 Com3 4 30 120
1. COUNT FUNCTION
◦ COUNT function is used to Count the
number of rows in a database table. It can
work on both numeric and non-numeric data
types.
◦ COUNT function uses the COUNT(*) that
returns the count of all the rows in a specified
table. COUNT(*) considers duplicate and
Null.
◦ Syntax
COUNT(*) or
COUNT( [ALL|DISTINCT] expression )
Example: COUNT()
SELECT COUNT(*) FROM PRODUCT_MAST;
Output:
10
12 Kathrin US
23 David Bangkok
34 Alina Dubai
45 John UK
56 Harry US
In Ascending Order:
SELECT * FROM CUSTOMER ORDER BY
NAME;
OUTPUT:
CUSTOMER_ID NAME ADDRESS
34 Alina Dubai
23 David Bangkok
56 Harry US
45 John UK
12 Kathrin US
In Descending Order:
SELECT * FROM CUSTOMER ORDER BY
NAME DESC;
OUTPUT:
CUSTOMER_ID NAME ADDRESS
12 Kathrin US
45 John UK
56 Harry US
23 David Bangkok
34 Alina Dubai
SQL LIKE Operator
The LIKE operator is used in a WHERE clause to search
for a specified pattern in a column.
There are two wildcards often used in conjunction with
the LIKE operator:
◦ % - The percent sign represents zero, one, or
multiple characters
◦ _ - The underscore represents a single
character
LIKE Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
Select emp_name , salary from employee where
emp_name like ‘v_i%i’;
Here are some examples showing different LIKE
operators with '%' and '_' wildcards:
LIKE Operator Description
WHERE CustomerName LIKE 'a%' Finds any values that start with "a"
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the
second position
WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and
are at least 2 characters in length
WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and
are at least 3 characters in length
WHERE CustomerName LIKE 'a%o' Finds any values that start with "a" and
ends with "o"
SQL IN Operator
The IN operator allows you to specify
multiple values in a WHERE clause.
The IN operator is a shorthand for
multiple OR conditions.
IN Syntax:
◦ SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
Example
SELECT * FROM Customers
WHERE ADDRESS IN (‘US', 'UK');
CUSTOMER_ID NAME ADDRESS
12 Kathrin US
45 John UK
56 Harry US
23 David Bangkok
34 Alina Dubai
SQL Built-In Functions:
1. Numeric Functions
2. Date Functions
3. String Functions
1. Numeric Functions:
Function Description Example
ABS() It returns the absolute value SELECT ABS(-243.5);
of a number. Output: 243.5
CEIL() It returns the smallest SELECT CEIL(25.75);
integer value that is greater Output: 26
than or equal to a number.
DIV() It is used for integer SELECT 10 DIV 5;
division. Output: 2
EXP() It returns e raised to the SELECT EXP(1);
power of number. Output: 2.718281
FLOOR() It returns the largest integer SELECT FLOOR(25.75);
value that is less than or Output: 25
equal to a number.
GREATEST() It returns the greatest value SELECT GREATEST(30, 2, 36,
in a list of expressions. 81, 125);
Output: 125
Function Description Example
LEAST() It returns the smallest SELECT LEAST(30, 2, 36,
value in a list of 81, 125);
expressions. Output: 2
1 Jack 3 Jackson
2 Harry 4 Stephan
3 Jackson 5 David
ID NAME
1 Jack
2 Harry
3 Jackson
4 Stephan
5 David
2. Intersect
The Intersect operation returns the common
rows from both the SELECT statements.
In the Intersect operation, the number of
datatype and columns must be the same.
It has no duplicates and it arranges the data in
ascending order by default.
Syntax
◦ SELECT column_name FROM table1INTERSECT
SELECT column_name FROM table2;
Example:
Using the above First and Second table.
Intersect query will be:
◦ SELECT * FROM First INTERSECT
SELECT * FROM Second;
Output:
ID NAME
3 Jackson
3. Minus
It combines the result of two SELECT
statements. Minus operator is used to display
the rows which are present in the first
query but absent in the second query.
It has no duplicates and data arranged in
ascending order by default.
Syntax:
◦ SELECT column_name FROM table1 MINUS
SELECT column_name FROM table2;
Example:
Using the above First and Second table.
Minus query will be:
◦ SELECT * FROM First MINUS
SELECT * FROM Second;
Output:
ID NAME
1 Jack
2 Harry
SQL Sub Query
A Sub-query is a query within another SQL query and
embedded within the WHERE clause.
Important Rule:
A sub-query can be placed in a number of SQL clauses like
WHERE clause, FROM clause, HAVING clause.
You can use Sub-query with SELECT, UPDATE, INSERT,
DELETE statements along with the operators like =, <, >, >=,
<=, IN, BETWEEN, etc.
A sub-query is a query within another query. The outer
query is known as the main query, and the inner query is
known as a sub-query.
Sub-queries are on the right side of the comparison
operator.
A sub-query is enclosed in parentheses.
In the Sub-query, ORDER BY command cannot be used.
1. Subqueries with the Select
Statement
SQL subqueries are most frequently used with
the Select statement.
Syntax:
◦ SELECTcolumn_name FROM table_name
WHERE column_name expression operator
( SELECT column_name from table_name
WHERE ... );
Consider the EMPLOYEE table:
1 John 20 US 2000.00
4 Alina 29 UK 6500.00
4 Alina 29 UK 6500.00
1 John 20 US 2000.00
2 Stephan 26 Dubai 1500.00
3 David 27 Bangkok 2000.00
7 Jackson 25 Mizoram 10000.00
SQL JOIN
As the name shows, JOIN means to combine
something. In case of SQL, JOIN means "to
combine two or more tables".
In SQL, JOIN clause is used to combine the
records from two or more tables in a database.
Types of SQL JOIN
1. INNER JOIN
2. LEFT JOIN
3. RIGHT JOIN
4. FULL JOIN
Consider the sample table Employee and
Project:
EMP_ID EMP_NAME CITY SALARY AGE
101 1 Testing
102 2 Development
103 3 Designing
104 4 Development
1. INNER JOIN
INNER JOIN selects records that have matching
values in both tables as long as the condition is
satisfied. It returns the combination of all rows
from both the tables where the condition
satisfies.
Query
◦ SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT FRO
M EMPLOYEE INNER JOIN PROJECT ON PROJECT.EMP_ID =
EMPLOYEE.EMP_ID;
Output: EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
2. LEFT JOIN
The SQL left join returns all the values from left table
and the matching values from the right table. If there is
no matching join value, it will return NULL.
Query:
SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT
FROM EMPLOYEE LEFT JOIN PROJECT
ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
O/p: EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
Russell NULL
Marry NULL
3. RIGHT JOIN
In SQL, RIGHT JOIN returns all the values from the
values from the rows of right table and the matched
values from the left table. If there is no matching in both
tables, it will return NULL.
Query:
SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT FROM
EMPLOYEE RIGHT JOIN PROJECT ON PROJECT.EMP_ID = EMP
LOYEE.EMP_ID;
O/p:
EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
4. FULL JOIN
In SQL, FULL JOIN is the result of a combination of
both left and right outer join. Join tables have all the
records from both tables. It puts NULL on the place of
matches not found.
Query:
SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT FROM
EMPLOYEE FULL JOIN PROJECT ON PROJECT.EMP_ID = EMPL
OYEE.EMP_ID;
O/P: EMP_NAME DEPARTMENT
Angelina Testing
Robert Development
Christian Designing
Kristen Development
Russell NULL
Marry NULL
Views in SQL
Views in SQL are considered as a virtual table.
A view also contains rows and columns.
To create the view, we can select the fields from
one or more tables present in the database.
A view can either have specific rows based on
certain condition or all the rows of a table.
How to create view:
A view can be created using the CREATE
VIEW statement.
Syntax:
CREATE VIEW view_name AS SELECT
column1, column2..... FROM table_name
WHERE condition;
Query:
CREATE VIEW DetailsView AS SELECT NAME,
ADDRESS FROM Student_Details
WHERE STU_ID < 4;
Just like table query, we can query the
view to view the data.
◦ SELECT * FROM DetailsView;
Output:
NAME ADDRESS
Stephan Delhi
Kathrin Noida
David Ghaziabad
Thank YOU