Dbms Practical 5-8
Dbms Practical 5-8
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:
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.
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
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 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 :
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.
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;
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
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:
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 -