0% found this document useful (0 votes)
12 views14 pages

Dbms Practical 5-8

The document provides practical SQL exercises on creating tables, inserting data, and using various SQL clauses such as ORDER BY and GROUP BY. It includes syntax for creating a customer table, inserting multiple records, and applying constraints. Additionally, it covers queries involving expressions, sorting results, and aggregating data based on groups.

Uploaded by

alantech845
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views14 pages

Dbms Practical 5-8

The document provides practical SQL exercises on creating tables, inserting data, and using various SQL clauses such as ORDER BY and GROUP BY. It includes syntax for creating a customer table, inserting multiple records, and applying constraints. Additionally, it covers queries involving expressions, sorting results, and aggregating data based on groups.

Uploaded by

alantech845
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

PRACTICAL 5

To create table and insert value


A Table is a combination of rows and columns. For creating a table we have to define the structure of a
table by adding names to columns and providing data type and size of data to be stored in columns.

Syntax:
CREATE table
table_name (
Column1 datatype (size),
column2 datatype (size),
………
columnN datatype(size)
);
Here table_name is name of the table, column is the name of column.

Let us create a table to store data of Customers, so the table name is Customer, Columns are Name,
Country, age, phone, and so on.
CREATE TABLE Customer(
CustomerID INT PRIMARY
KEY,
CustomerName VARCHAR(50),
LastName VARCHAR(50),
Country VARCHAR(50),
Age int(2),
Phone int(10)
);
Output:

Insert Data into Table


data to the table, we use INSERT INTO, the syntax is as shown below:

Syntax:
Insert into Table_name(Column1, Column2, Column3)
Values (Value1, value2, value3);
//Below query adds data in table in sequence of column name(Value1 will be added in Column1 and so
on)//
Insert into Table_name
Values (Value1, value2, value3);
//Adding multiple data in the table in one go//
Insert into Table_name
Values (Value01, value02,
value03), (Value11, value12,
value13), (Value21, value22,
value23), (ValueN1, valueN2,
valueN3)

Example Query
This query will add data in the table named Subject
-- Insert some sample data into the Customers table
INSERT INTO Customer (CustomerID, CustomerName, LastName, Country, Age, Phone)
VALUES (1, 'Shubham', 'Thakur', 'India','23','xxxxxxxxxx'),
(2, 'Aman ', 'Chopra', 'Australia','21','xxxxxxxxxx'),
(3, 'Naveen', 'Tulasi', 'Sri lanka','24','xxxxxxxxxx'),
(4, 'Aditya', 'Arpan', 'Austria','21','xxxxxxxxxx'),
(5, 'Nishant. Salchichas S.A.', 'Jain', 'Spain','22','xxxxxxxxxx');

Output:
SQL Constraints
SQL constraints are used to specify rules for the data in a
table. The following constraints are commonly used in SQL:
 NOT NULL - Ensures that a column cannot have a NULL value
 UNIQUE - Ensures that all values in a column are different
 PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each
row in a table
 FOREIGN KEY - Prevents actions that would destroy links between tables
 CHECK - Ensures that the values in a column satisfies a specific condition
 DEFAULT - Sets a default value for a column if no value is specified
 CREATE INDEX - Used to create and retrieve data from the database very quickly
PRACTICAL 6
Some Query with expression

A query with an expression is a SQL query that contains an expression in the SELECT clause. An
expression is a combination of operands and operators that evaluates to a single value.
Operands can be columns, constants, or other expressions. Operators can be arithmetic operators, logical
operators, or comparison operators.

SYSDATE  System Date


DUAL  DUAL is a one-row, one-column built-in table in Oracle Database. It is used to hold the
results of a SELECT statement that are otherwise not stored or used.

Alias Name
An alias name in SQL is a temporary name that can be given to a column or table in a query.
Aliases are used to make column or table names more readable or to avoid ambiguity.
To create an alias in SQL, you use the AS keyword.

Query 1
ENAME SAL PF
List name, salary and PF amount of all the employees PF is an 10% of salary. KING 5000 500
BLAKE 2850 285
SELECT ename, sal, sal*.1 "PF" FROM emp; CLARK 2450 245
JONES 2975 297.5
Output: SCOTT 3000 300
FORD 3000 300
SMITH 800 80
ALLEN 1600 160
WARD 1250 125
MARTIN 1250 125
TURNER 1500 150
ADAMS 1100 110
JAMES 950 95
Query 2 MILLER 1300 130

List the name of employees who are more than 41 years old in the organization.
SELECT ename, (SYSDATE-HIREDATE)/365 "HY" FROM emp where (SYSDATE-
HIREDATE)/365>41
ENAME HY
KING 41.86929845256215119228817858954845256219
BLAKE 42.41724365804160324708269913749365804164
CLARK 42.31039434297311009639776763064434297315
JONES 42.49669571283612379502790461694571283616
Output : FORD 41.82546283612379502790461694571283612384
SMITH 42.78710667174023338406900050735667174027
ALLEN 42.60902447995941146626078132927447995945
WARD 42.60354502790461694571283612379502790466
MARTIN 42.00628475393201420598680872653475393205
TURNER 42.06107927447995941146626078132927448
JAMES 41.82546283612379502790461694571283612384
MILLER 41.68573680872653475393201420598680872658
Query 3
List the name of employee name, salary, PF, HRA, DA, and GROSS.
PF = 10%SAL
HRA = 50%SAL
DA = 30%SAL
GROSS = SAL + HRA + DA – PF

Query
select ename, sal, sal*.1 "PF", sal*0.2 "HRA", sal*0.3 "DA" from emp

ENAME SAL PF HRA DA GROSS


Output : KING 5000 500 2500 1500 8500
BLAKE 2850 285 1425 855 4845
CLARK 2450 245 1225 735 4165
JONES 2975 297.5 1487.5 892.5 5057.5
SCOTT 3000 300 1500 900 5100
FORD 3000 300 1500 900 5100
SMITH 800 80 400 240 1360
ALLEN 1600 160 800 480 2720
WARD 1250 125 625 375 2125
MARTIN 1250 125 625 375 2125
TURNER 1500 150 750 450 2550
ADAMS 1100 110 550 330 1870
JAMES 950 95 475 285 1615
MILLER 1300 130 650 390 2210
PRACTICAL 6
Using ORDER BY clause
The ORDER BY clause in SQL is used to sort the results of a query in ascending or descending order.
The ORDER BY clause can be used to sort the results of a query by one or more columns.
 To sort the results of a query in ascending order, you use the ASC keyword.
 To sort the results of a query in descending order, you use the DESC keyword.

Syntax
SELECT * FROM table_name ORDER BY column1 ASC|DESC , column2 ASC|DESC

Query 1
List the employee no name and salary in ascending order of salary. select EMPNO ENAME SAL
7369 SMITH 800
empno, ename, sal from emp
7900 JAMES 950
order by(3) 7876 ADAMS 1100
7654 MARTIN 1250
Output : 7521 WARD 1250
7934 MILLER 1300
7844 TURNER 1500
7499 ALLEN 1600
7782 CLARK 2450
7698 BLAKE 2850
7566 JONES 2975
7902 FORD 3000
7788 SCOTT 3000
7839 KING 5000

Query 2 ENAME JOB


WARD SALESMAN
List the employee name and the jobs in descending order of the names. select TURNER SALESMAN
SMITH CLERK
ename, job from emp
SCOTT ANALYST
order by 1 desc MILLER CLERK
MARTIN SALESMAN
Output : KING PRESIDENT
JONES MANAGER
JAMES CLERK
FORD ANALYST
CLARK MANAGER
BLAKE MANAGER
ALLEN SALESMAN
ADAMS CLERK

Query 3
List the employee name and hiredate in descending order of hiredate in the result show hiredate and
date of joining.
select ename, hiredate "Date of Joining" from emp
order by 2 desc
Output : ENAME Date of Joining
ADAMS 23-MAY-87
SCOTT 19-APR-87
MILLER 23-JAN-82
FORD 03-DEC-81
JAMES 03-DEC-81
KING 17-NOV-81
MARTIN 28-SEP-81
TURNER 08-SEP-81
CLARK 09-JUN-81
BLAKE 01-MAY-81
JONES 02-APR-81
WARD 22-FEB-81
ALLEN 20-FEB-81
SMITH 17-DEC-80

Query 4
List the name of employee name, salary, PF, HRA, DA, and GROSS.
PF = 10%SAL
HRA = 50%SAL
DA = 30%SAL
GROSS = SAL + HRA + DA – PF
select ename, sal,sal*0.1 "PF", sal*0.5 "HRA", sal*0.3 "DA", sal+sal*0.5+ sal*0.3-sal*0.1
"GROSS" from emp
order by 6 desc

Output :

ENAME SAL PF HRA DA GROSS


KING 5000 500 2500 1500 8500
SCOTT 3000 300 1500 900 5100
FORD 3000 300 1500 900 5100
JONES 2975 297.5 1487.5 892.5 5057.5
BLAKE 2850 285 1425 855 4845
CLARK 2450 245 1225 735 4165
ALLEN 1600 160 800 480 2720
TURNER 1500 150 750 450 2550
MILLER 1300 130 650 390 2210
MARTIN 1250 125 625 375 2125
WARD 1250 125 625 375 2125
ADAMS 1100 110 550 330 1870
JAMES 950 95 475 285 1615
SMITH 800 80 400 240 1360
PRACTICAL 7
Using GROUP BY clause

The GROUP BY clause in SQL is used to group the rows in a table based on the values in one or more
columns. This can be useful for performing calculations on the groups of rows, such as counting the
number of rows in each group, calculating the average value in each group, or finding the maximum or
minimum value in each group.
To use the GROUP BY clause, you specify the columns that you want to group the rows by in the
GROUP BY clause. You can also specify aggregate functions in the SELECT clause to perform
calculations on the groups of rows.
The GROUP BY clause can also be used to group the rows in a table by multiple columns. To do this,
you specify the columns that you want to group the rows by in the GROUP BY clause, separated by
commas.

Some points in GROUP BY clause in SQL:


 The GROUP BY clause can be used with the HAVING clause to filter the groups of rows.
 The GROUP BY clause can be used with the ORDER BY clause to sort the groups of rows.
 The GROUP BY clause can be used with the LIMIT clause to limit the number of groups of
rows that are returned.

Having clause
The HAVING clause in SQL is used to filter the results of a query after the GROUP BY clause has
been applied. The HAVING clause can be used to filter the groups of rows based on the results of
aggregate functions.
To use the HAVING clause, you specify the condition that the groups of rows must meet in the
HAVING clause. The condition can be a comparison operator, a logical operator, or a combination of
both.

Query 1
List the dept. no and no of employees in each department.
select dname, count(*) count_of_employees
from dept, emp
where dept.deptno = emp.deptno group by
DNAME
DNAME COUNT_OF_EMPLOYEES
order by 2 desc SALES 6
RESEARCH 5
Output : ACCOUNTING 3

Query 2
List the dept. no and total salary payable in each dept.
select deptno, sum(sal) from emp group by
deptno DEPTNO SUM(SAL)
30 9400
Output : 10 8750
20 10875

Query 3
List the jobs and no of employees in each jobs the result should be in descending order in the no of
employees.
select job, count(empno) "EMPNO" from emp group by job JOB EMPNO
SALESMAN 4
order by EMPNO desc CLERK 4
MANAGER 3
ANALYST 2
PRESIDENT 1
Query 4
List the total max, min and average salary of employees of jobwise.
select job, max(sal) "Maximum", min(sal) "Minimum", avg(sal) "Average" from emp group by job;

Output : JOB Maximum Minimum Average


ANALYST 3000 3000 3000
CLERK 1300 800 1037.5
SALESMAN 1600 1250 1400
MANAGER 2975 2450 2758.333333333333333333333333333333333333
PRESIDENT 5000 5000 5000
Query 5
List the total max, min and average salary of employees for jobwise for dept. no 20.
select job, max(sal) "Maximum", min(sal) "Minimum", avg(sal) "Average" from emp where deptno=20
group by job JOB Maximum Minimum Average
ANALYST 3000 3000 3000
Output : CLERK 1100 800 950
MANAGER 2975 2975 2975

Query 6
List the total max, min and average salary of employees for jobwise for dept. no 20 and display those
row having average salary>1000.
select job, max(sal) "Maximum", min(sal) "Minimum", avg(sal) "Average" from emp
where deptno=20
group by job
having avg(sal)>1000
JOB Maximum Minimum Average
ANALYST 3000 3000 3000
Output : MANAGER 2975 2975 2975
10

PRACTICAL 8
Using all JOIN operations
SQL Join statement is used to combine data or rows from two or more tables based on a common field
between them.

Syntax
SELECT <col list>
FROM <table1, table2,- - -, table_n>
WHERE table1_col1 = table2_col2
AND table2_col3 = table3_col4
AND table_n-1_col1_n-1 = table_n_col_n

Equi Join
An equi join, also known as an equality join, is a type of join in SQL that combines rows from two or
more tables based on the equality of the values in one or more columns. Equi joins are the most
common type of join, and they are used in a wide variety of SQL queries.

Query
List the employee no, name, dept. no and dept. names.
select emp.empno, emp.ename, dept.deptno, dept.dname from emp, dept
where emp.deptno = dept.deptno;

Output :
EMPNO ENAME DEPTNO DNAME
7839 KING 10 ACCOUNTING
7698 BLAKE 30 SALES
7782 CLARK 10 ACCOUNTING
7566 JONES 20 RESEARCH
7788 SCOTT 20 RESEARCH
7902 FORD 20 RESEARCH
7369 SMITH 20 RESEARCH
7499 ALLEN 30 SALES
7521 WARD 30 SALES
7654 MARTIN 30 SALES
7844 TURNER 30 SALES
7876 ADAMS 20 RESEARCH
7900 JAMES 30 SALES
7934 MILLER 10 ACCOUNTING

Cartesian product join


A Cartesian product join, also known as a cross join, is a type of join in SQL that returns all possible
combinations of rows from two or more tables. Cartesian product joins are not as common as equi
joins, but they can be useful in certain situations, such as when you need to generate all possible
combinations of data for a report.
11

Query
List the employee no, name, dept. name and location.
select emp.empno, emp.ename, dept.deptno, dept.dname, dept.loc from emp, dept
EMPNO ENAME DEPTNO DNAME LOC
7839 KING 10 ACCOUNTING NEW YORK
7698 BLAKE 10 ACCOUNTING NEW YORK
7782 CLARK 10 ACCOUNTING NEW YORK
7566 JONES 10 ACCOUNTING NEW YORK
7788 SCOTT 10 ACCOUNTING NEW YORK
7902 FORD 10 ACCOUNTING NEW YORK
7369 SMITH 10 ACCOUNTING NEW YORK
7499 ALLEN 10 ACCOUNTING NEW YORK
7521 WARD 10 ACCOUNTING NEW YORK
7654 MARTIN 10 ACCOUNTING NEW YORK
7844 TURNER 10 ACCOUNTING NEW YORK
7876 ADAMS 10 ACCOUNTING NEW YORK
7900 JAMES 10 ACCOUNTING NEW YORK
7934 MILLER 10 ACCOUNTING NEW YORK
7839 KING 20 RESEARCH DALLAS
7698 BLAKE 20 RESEARCH DALLAS
7782 CLARK 20 RESEARCH DALLAS
7566 JONES 20 RESEARCH DALLAS
7788 SCOTT 20 RESEARCH DALLAS
7902 FORD 20 RESEARCH DALLAS
7369 SMITH 20 RESEARCH DALLAS
7499 ALLEN 20 RESEARCH DALLAS
7521 WARD 20 RESEARCH DALLAS
7654 MARTIN 20 RESEARCH DALLAS
7844 TURNER 20 RESEARCH DALLAS
7876 ADAMS 20 RESEARCH DALLAS
7900 JAMES 20 RESEARCH DALLAS
7934 MILLER 20 RESEARCH DALLAS
7839 KING 30 SALES CHICAGO
7698 BLAKE 30 SALES CHICAGO
7782 CLARK 30 SALES CHICAGO
7566 JONES 30 SALES CHICAGO
7788 SCOTT 30 SALES CHICAGO
7902 FORD 30 SALES CHICAGO
7369 SMITH 30 SALES CHICAGO
7499 ALLEN 30 SALES CHICAGO
7521 WARD 30 SALES CHICAGO
7654 MARTIN 30 SALES CHICAGO
7844 TURNER 30 SALES CHICAGO
7876 ADAMS 30 SALES CHICAGO
7900 JAMES 30 SALES CHICAGO
7934 MILLER 30 SALES CHICAGO
7839 KING 40 OPERATIONS BOSTON
7698 BLAKE 40 OPERATIONS BOSTON
7782 CLARK 40 OPERATIONS BOSTON
7566 JONES 40 OPERATIONS BOSTON
7788 SCOTT 40 OPERATIONS BOSTON
7902 FORD 40 OPERATIONS BOSTON
7369 SMITH 40 OPERATIONS BOSTON
7499 ALLEN 40 OPERATIONS BOSTON

Outer Join
An outer join in SQL is a type of join that returns all rows from one or both tables, even if there are no
matching rows in the other table. There are three types of outer joins:

 LEFT JOIN: Returns all rows from the left table, even if there is no matching row in the right table.
12

 RIGHT JOIN: Returns all rows from the right table, even if there is no matching row in the left table.
 FULL OUTER JOIN: Returns all rows from both tables, even if there is no matching row in the
other table.

To perform an outer join, you use the JOIN clause to specify the tables to join and the columns to join them on.
You can also use the ON clause to specify additional conditions that the rows must meet in order to be joined.

Query
Display the list of employee working in each department and display the department information even
if no employee belongs to the department.
select * from emp, dept
where emp.deptno (+)= dept.deptno

Output :
EMPN ENAM JOB MG HIREDAT SA COM DEPTN DEPTN DNAME LOC
O E R E L M O O
7839 KING PRESIDEN - 17-NOV- 500 - 10 10 ACCOUNTIN NEW
T 81 0 G YORK
7698 BLAKE MANAGE 7839 01-MAY- 285 - 30 30 SALES CHICAG
R 81 0 O
7782 CLARK MANAGE 7839 09-JUN-81 245 - 10 10 ACCOUNTIN NEW
R 0 G YORK
7566 JONES MANAGE 7839 02-APR-81 297 - 20 20 RESEARCH DALLAS
R 5
7788 SCOTT ANALYST 7566 19-APR-87 300 - 20 20 RESEARCH DALLAS
0
7902 FORD ANALYST 7566 03-DEC-81 300 - 20 20 RESEARCH DALLAS
0
7369 SMITH CLERK 7902 17-DEC-80 800 - 20 20 RESEARCH DALLAS
7499 ALLEN SALESMA 7698 20-FEB-81 160 300 30 30 SALES CHICAG
N 0 O
7521 WARD SALESMA 7698 22-FEB-81 125 500 30 30 SALES CHICAG
N 0 O
7654 MARTI SALESMA 7698 28-SEP-81 125 1400 30 30 SALES CHICAG
N N 0 O
7844 TURNE SALESMA 7698 08-SEP-81 150 0 30 30 SALES CHICAG
R N 0 O
7876 ADAM CLERK 7788 23-MAY- 110 - 20 20 RESEARCH DALLAS
S 87 0
7900 JAMES CLERK 7698 03-DEC-81 950 - 30 30 SALES CHICAG
O
7934 MILLE CLERK 7782 23-JAN-82 130 - 10 10 ACCOUNTIN NEW
R 0 G YORK
- - - - - - - - 40 OPERATION BOSTON
S

Self Join
A self join is a type of join in SQL that joins a table to itself. This can be useful for a variety of tasks, such as:

 Finding hierarchical relationships in a table


 Finding duplicate rows in a table
 Finding rows in a table that are related to themselves
13

To perform a self join, you use the JOIN clause to specify the table to join to itself and the columns to join on.
You can also use the ON clause to specify additional conditions that the rows must meet in order to be joined.

Query
Give the names of employees along with the name of the manager.
select worker.ename, manager.ename from emp worker, emp manager where
worker.mgr = manager.empno ENAME ENAME
BLAKE KING
Output : CLARK KING
JONES KING
ALLEN BLAKE
WARD BLAKE
MARTIN BLAKE
TURNER BLAKE
JAMES BLAKE
MILLER CLARK
SCOTT JONES
FORD JONES
ADAMS SCOTT
SMITH FORD

Query
Give the names of the employees along with the name of the manager with the president. select
worker.ename "employees", manager.ename "manager" from emp worker, emp manager where
worker.mgr = manager.empno(+)
employees manager
Output : BLAKE KING
CLARK KING
JONES KING
ALLEN BLAKE
WARD BLAKE
MARTIN BLAKE
TURNER BLAKE
JAMES BLAKE
MILLER CLARK
SCOTT JONES
FORD JONES
ADAMS SCOTT
SMITH FORD
KING -

You might also like