Lab 5
Lab 5
Lab 5
LAB 5
DML (Data Manipulation Language):
Interact with data records “rows” itself .
Commands:
o Insert
o Update
o Delete
o Select
For example:
For example:
For Example, Consider a scenario where we have to periodically move data from our existing table to
a historic or archive table. In order to achieve this, let’s create a new table employees_history.
Our task is to move data from the employee table to the employees_history table.
First, we will create employees_history with the same columns of employee table.
Second, you need to select all data from employee table and insert it into employee_history table.
UPDATE Statement:
The UPDATE statement is used to modify the existing records in a table.
Delete Statement:
Select Statement:
If you want to select all the fields available in the table, use the following syntax:
SELECT * FROM table_name;
For example: Display the full name of the emplo (by using Concatenation).
SELECT concat(fname, ' ' ,lname) As full_name
From employee;
CSCI305-FALL
E.g.:
CSCI305-FALL
Alias concept:
DISTINCT:
CSCI305-FALL
Aggregate functions
o Min()
o Max()
o Count()
o Avg()
o Sum()
ORDER BY:
The ORDER BY keyword sorts the records in ascending order by default. To sort
the
records in descending order, use the DESC keyword.
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
LIMIT:
The LIMIT command is used to specify the number of records to return.
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;
CSCI305-FALL
Group by:
The GROUP BY statement groups rows that have the same values into summary
rows, like "find the number of customers in each country".
select avg(salary)
from employee
group by Dno
Example: For each department, retrieve the department number, the number of
employees in the department, and their average salary.
Having:
The HAVING clause was added to SQL because the WHERE keyword cannot be used
with aggregate functions.
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
CSCI305-FALL
Example: Display the average salary for each department given that its maximum
salary is greater than 1800
select avg(salary)
from employee
group by Dno
having max(salary) > 1800;
Set operations:
SQL has directly incorporated some set operations
o There is a union operation (UNION),
o and in some versions of SQL (MINUS) and intersection (INTERSECT)
The resulting relations of these set operations are sets of tuples;
o duplicate tuples are eliminated from the result
The set operations apply only to union compatible relations;
o the two relations must have the same attributes and the attributes must
appear in the same order
CSCI305-FALL
Union:
INTERSECT:
CSCI305-FALL
Minus:
CSCI305-FALL
Example:
Solutions:
1. SELECT * FROM Dependent;
2. UPDATE Employee
set Salary=12000
where Ssn=3;
3. Update Works_On
set hours=8
where Essn=2 and Pno=125 ;
Hint: this will give an error “Safe update mode”, to remove it go to “Edit” tab “Preferences” ”SQL
editor” uncheck “safe updates….” “Query” tab “reconnect to server”
6. SELECT SUM(Salary)
From Employee;
Lab 5 Exercise