5. Structured Query Language (SQL)for Final Exam
5. Structured Query Language (SQL)for Final Exam
LAB SESSION
SQL LANGUAGE
Introduction
Structured Query Language (SQL) is a query language that is
standardized for RDBMS.
SQL Statements (commonly referred to as 'queries') are run to
retrieve or modify the requested information from the database
SQL supports:
Data Definition Language (DDL), and
Data Manipulation Language (DML)
DQL (Data Query Language)
DCL (Data Control Language)
TCL (Transaction Control Language)
2
Why SQL?
3
…………………………………..
sub_id int,
Sub_name varchar(20)
)
9
ALTER TABLE – ADD Column
10
Syntax:
Example:
ADD(AGE)
11 ALTER TABLE – DROP
DROP COLUMN is used to drop columns in a table.
Deleting the unwanted columns from the table.
Syntax:
Syntax:
It is used to delete the entities inside the table while holding the
structure of the table. It free up the space from the table whereas
the column names are left resulting to store new data in the
table
Syntax:
15
INSERT INTO Statement
16
The INSERT INTO statement of SQL is used to insert a new
row/record in a table. There are two ways of using the INSERT
INTO statement for inserting rows.
Only Values
Syntax:
Syntax:
Syntax:
INDEX: Use to create and retrieve data from the database very
quickly.
NOT NULL Constraint:
23
Primary keys must contain unique values. A primary key column cannot have
NULL values.
A table can have only one primary key, which may consist of single or
multiple fields.
When multiple fields are used as a primary key, they are called a composite
key. If a table has a primary key defined on any field(s), then you can not have
two records having the same value of that field(s).
Note: You would use these concepts while creating database tables.
30 EXAMPLE
CREATE TABLE CUSTOMERS(
CUSTOMERS table
ORDERS table:
DATE DATETIME,
AMOUNT double,
ON CUSTOMERS ( AGE );
40 SORTING
Sorting (ascending, descending) using ORDER BY, DESC
and ASC.
SQL ORDER BY Clause:
SELECT column1, column2....column
FROM table_name
ORDER BY column_name {ASC|DESC};
41 SQL Joins
LEFT JOIN: returns all rows from the left table, even if there
are no matches in the right table.
RIGHT JOIN: returns all rows from the right table, even if
there are no matches in the left table.
The SQL LEFT JOIN returns all rows from the left table, even if
there are no matches in the right table. This means that if the ON
clause matches 0 (zero) records in right table, the join will still
return a row in the result, but with NULL in each column from
right table.
Syntax :
SELECT table1.column1, table2.column2...
FROM table1
LEFT JOIN table2
ON table1.common_filed = table2.common_field;
RIGHT JOIN
45
The SQL RIGHT JOIN returns all rows from the right table,
even if there are no matches in the left table. This means that if
the ON clause matches 0 (zero) records in left table, the join
will still return a row in the result, but with NULL in each
column from left tables.
Syntax :
SELECT table1.column1, table2.column2...
FROM table1
RIGHT JOIN table2
ON table1.common_filed = table2.common_field;
FULL JOIN
46
The SQL FULL JOIN combines the results of both left and
right outer joins. The joined table will contain all records from
both tables, and fill in NULLs for missing matches on either
side
Syntax:
FROM table1
ON table1.common_filed = table2.common_field;
47 Example (JOINS)
Student table
Roll no Name Age Address Phone_no
1 Ramat 32 ABC 0925544763
2 Burtukan 25 Delhi 0911125896
3 Kedir 23 UK 0952874113
4 Zola 25 USA 0947772123
5 Tola 22 ETH 0987456321
StudentCourse table
Course_id Roll no
102 1
100 2
101 3
103 4
48 SQL Aggregate Functions
An aggregate function is a function that performs a calculation on a set of
values, and returns a single value.
COUNT function uses the COUNT(*) that returns the count of all the
rows in a specified table.
Syntax:
COUNT(*)
or
COUNT( [ALL|DISTINCT] expression )
50 Example
Product_mast table
51 Example …
SELECT COUNT(*)
FROM product_mast;
COUNT with WHERE
SELECT COUNT(*)
FROM PRODUCT_MAST;
WHERE RATE>=20;
COUNT() with GROUP BY
SELECT COMPANY, COUNT(*)
FROM PRODUCT_MAST
GROUP BY COMPANY;
52 SUM Function
Sum function is used to calculate the sum of all selected
columns. It works on numeric fields only.
Syntax:
SUM()
or
SUM( [ALL|DISTINCT] expression )
53 Example: SUM()
SELECT SUM(COST)
FROM PRODUCT_MAST;
SUM() with WHERE
SELECT SUM(COST)
FROM PRODUCT_MAST
WHERE QTY>3;
SUM() with GROUP BY
SELECT SUM(COST)
FROM PRODUCT_MAST
WHERE QTY>3
GROUP BY COMPANY;
54 AVG function
The AVG function is used to calculate the average value of the
numeric type. AVG function returns the average of all non-Null
values.
Syntax:
AVG()
or
AVG( [ALL|DISTINCT] expression )
Example
SELECT AVG(COST)
FROM PRODUCT_MAST;
55 MAX function
MAX function is used to find the maximum value of a certain column. This
function determines the largest value of all selected values of a column.
Syntax:
MAX()
or
MAX( [ALL|DISTINCT] expression )
Example:
SELECT MAX(RATE)
FROM PRODUCT_MAST;
56 MIN function
MIN function is used to find the minimum value of a certain
column. This function determines the smallest value of all
selected values of a column.
Syntax:
MIN()
or
MIN( [ALL|DISTINCT] expression )
Example:
SELECT MIN(RATE)
FROM PRODUCT_MAST;
57 The SQL UNION Operator
Example
Now, let us retrieve the name, age, and hobby of students aged
between 25 and 30 from both the 'STUDENTS' and
'STUDENTS_HOBBY' tables, returning only the common rows
within the specified age range −
62 Conts’…
SELECT NAME, AGE, HOBBY
FROM STUDENTS_HOBBY
INTERSECT
QUERY:
Output: 4
3. UPPER() and LOWER(): Convert
68 Text Case
These functions convert the text to uppercase or lowercase,
respectively. They are useful for normalizing the case of text
in a database.
Query:
Output:
ABCD
abcd
69 4. LENGTH(): Length of String in Bytes
Query:
output: 5
70 5. LEFT() and RIGHT(): Extract Substring from
Left or Right
QUERY: