You are on page 1of 6

Name:- Madhavai Sapna Madhav

Roll No.- 04
Batch - T1

Practicle 03-
Design & develop SQL queries for suitable database application using SQL
DML statements which demonstrate the use of concept like all types of JOIN and view

SHOW DATABASES:-
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| Vaishnavi |
| abc |
| college |
| comp |
| computer |
| emp_info |
| info |
| moin |
| moinshaikh |
| moya |
| mysql |
| performance_schema |
| sndcoe |
| stud |
| student |
| student1 |
| te |
| tecomp |
+--------------------+
19 rows in set (0.07 sec)
************************************************************************************************

USE DATABASE:-
mysql> use sndcoe;
Database changed

mysql> INSERT INTO empl_tbl(emp_id, name, DOB, salary, Address) VALUES(1, 'Sm', '2002-10-02',
12000, 'Rayate'), (2, 'Tm', '2002-11-11', 20000, 'Rahata'), (3, 'Gb', '2002-02-02', 20001,
'Gavandgaon'), (4, 'Ak', '2002-10-10', 50000, 'Dhule'), (5, 'Tg', '2002-12-02', 30000,
'Dhulgaon');Query OK, 5 rows affected (0.06 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> insert into acc(emp_id, acc_no, acc_type) values(1, 234646, 'saving'), (2, 4243743,
'current'), (3, 582347, 'saving'), (4, 53815, 'saving'), (5, 535858, 'saving');
Query OK, 5 rows affected (0.02 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> select *from empl_tbl;


+--------+------+------------+--------+------------+
| emp_id | name | DOB | salary | Address |
+--------+------+------------+--------+------------+
| 1 | Sm | 2002-10-02 | 12000 | Rayate |
| 2 | Tm | 2002-11-11 | 20000 | Rahata |
| 3 | Gb | 2002-02-02 | 20001 | Gavandgaon |
| 4 | Ak | 2002-10-10 | 50000 | Dhule |
| 5 | Tg | 2002-12-02 | 30000 | Dhulgaon |
+--------+------+------------+--------+------------+
5 rows in set (0.00 sec)

mysql> select *from acc;


+--------+---------+----------+
| emp_id | acc_no | acc_type |
+--------+---------+----------+
| 1 | 234646 | saving |
| 2 | 4243743 | current |
| 3 | 582347 | saving |
| 4 | 53815 | saving |
| 5 | 535858 | saving |
+--------+---------+----------+
5 rows in set (0.00 sec)
************************************************************************************************

AGGREGATES
A) AVERAGE:-
mysql> select avg(salary) from empl_tbl;
+-------------+
| avg(salary) |
+-------------+
| 26400.2000 |
+-------------+
1 row in set (0.00 sec)

B)MINIMUM:-
mysql> select min(salary) from empl_tbl;
+-------------+
| min(salary) |
+-------------+
| 12000 |
+-------------+
1 row in set (0.03 sec)

C)MAXIMUM:-
mysql> select max(salary) from empl_tbl;
+-------------+
| max(salary) |
+-------------+
| 50000 |
+-------------+
1 row in set (0.00 sec)
************************************************************************************************

BETWEEN:-
mysql> select *from empl_tbl WHERE salary BETWEEN 20000 AND 30000;
+--------+------+------------+--------+------------+
| emp_id | name | DOB | salary | Address |
+--------+------+------------+--------+------------+
| 2 | Tm | 2002-11-11 | 20000 | Rahata |
| 3 | Gb | 2002-02-02 | 20001 | Gavandgaon |
| 5 | Tg | 2002-12-02 | 30000 | Dhulgaon |
+--------+------+------------+--------+------------+
3 rows in set (0.00 sec)
************************************************************************************************

JOINS
1)INNER JOIN:-

mysql> SELECT * from empl_tbl INNER JOIN acc on empl_tbl.emp_id=acc.emp_id;


+--------+------+------------+--------+------------+--------+---------+----------+
| emp_id | name | DOB | salary | Address | emp_id | acc_no | acc_type |
+--------+------+------------+--------+------------+--------+---------+----------+
| 1 | Sm | 2002-10-02 | 12000 | Rayate | 1 | 234646 | saving |
| 2 | Tm | 2002-11-11 | 20000 | Rahata | 2 | 4243743 | current |
| 3 | Gb | 2002-02-02 | 20001 | Gavandgaon | 3 | 582347 | saving |
| 4 | Ak | 2002-10-10 | 50000 | Dhule | 4 | 53815 | saving |
| 5 | Tg | 2002-12-02 | 30000 | Dhulgaon | 5 | 535858 | saving |
+--------+------+------------+--------+------------+--------+---------+----------+
5 rows in set (0.01 sec)

2)OUTER JOIN:-
a) LEFT OUTER JOIN
mysql> select *from empl_tbl LEFT OUTER JOIN acc on empl_tbl.emp_id=acc.emp_id;
+--------+------+------------+--------+------------+--------+---------+----------+
| emp_id | name | DOB | salary | Address | emp_id | acc_no | acc_type |
+--------+------+------------+--------+------------+--------+---------+----------+
| 1 | Sm | 2002-10-02 | 12000 | Rayate | 1 | 234646 | saving |
| 2 | Tm | 2002-11-11 | 20000 | Rahata | 2 | 4243743 | current |
| 3 | Gb | 2002-02-02 | 20001 | Gavandgaon | 3 | 582347 | saving |
| 4 | Ak | 2002-10-10 | 50000 | Dhule | 4 | 53815 | saving |
| 5 | Tg | 2002-12-02 | 30000 | Dhulgaon | 5 | 535858 | saving |
+--------+------+------------+--------+------------+--------+---------+----------+
5 rows in set (0.00 sec)

b)RIGHT OUTER JOIN


mysql> select *from empl_tbl RIGHT OUTER JOIN acc on empl_tbl.emp_id=acc.emp_id;
+--------+------+------------+--------+------------+--------+---------+----------+
| emp_id | name | DOB | salary | Address | emp_id | acc_no | acc_type |
+--------+------+------------+--------+------------+--------+---------+----------+
| 1 | Sm | 2002-10-02 | 12000 | Rayate | 1 | 234646 | saving |
| 2 | Tm | 2002-11-11 | 20000 | Rahata | 2 | 4243743 | current |
| 3 | Gb | 2002-02-02 | 20001 | Gavandgaon | 3 | 582347 | saving |
| 4 | Ak | 2002-10-10 | 50000 | Dhule | 4 | 53815 | saving |
| 5 | Tg | 2002-12-02 | 30000 | Dhulgaon | 5 | 535858 | saving |
+--------+------+------------+--------+------------+--------+---------+----------+
5 rows in set (0.00 sec)
3) FULL JOIN
mysql> SELECT * FROM empl_tbl LEFT OUTER JOIN acc on empl_tbl.emp_id=acc.emp_id UNION select *
from empl_tbl RIGHT OUTER JOIN acc on empl_tbl.emp_id=acc.emp_id;
+--------+------+------------+--------+------------+--------+---------+----------+
| emp_id | name | DOB | salary | Address | emp_id | acc_no | acc_type |
+--------+------+------------+--------+------------+--------+---------+----------+
| 1 | Sm | 2002-10-02 | 12000 | Rayate | 1 | 234646 | saving |
| 2 | Tm | 2002-11-11 | 20000 | Rahata | 2 | 4243743 | current |
| 3 | Gb | 2002-02-02 | 20001 | Gavandgaon | 3 | 582347 | saving |
| 4 | Ak | 2002-10-10 | 50000 | Dhule | 4 | 53815 | saving |
| 5 | Tg | 2002-12-02 | 30000 | Dhulgaon | 5 | 535858 | saving |
+--------+------+------------+--------+------------+--------+---------+----------+
5 rows in set (0.00 sec)

4)CROSS JOIN
mysql> select *from empl_tbl CROSS JOIN acc;
+--------+------+------------+--------+------------+--------+---------+----------+
| emp_id | name | DOB | salary | Address | emp_id | acc_no | acc_type |
+--------+------+------------+--------+------------+--------+---------+----------+
| 1 | Sm | 2002-10-02 | 12000 | Rayate | 1 | 234646 | saving |
| 2 | Tm | 2002-11-11 | 20000 | Rahata | 1 | 234646 | saving |
| 3 | Gb | 2002-02-02 | 20001 | Gavandgaon | 1 | 234646 | saving |
| 4 | Ak | 2002-10-10 | 50000 | Dhule | 1 | 234646 | saving |
| 5 | Tg | 2002-12-02 | 30000 | Dhulgaon | 1 | 234646 | saving |
| 1 | Sm | 2002-10-02 | 12000 | Rayate | 2 | 4243743 | current |
| 2 | Tm | 2002-11-11 | 20000 | Rahata | 2 | 4243743 | current |
| 3 | Gb | 2002-02-02 | 20001 | Gavandgaon | 2 | 4243743 | current |
| 4 | Ak | 2002-10-10 | 50000 | Dhule | 2 | 4243743 | current |
| 5 | Tg | 2002-12-02 | 30000 | Dhulgaon | 2 | 4243743 | current |
| 1 | Sm | 2002-10-02 | 12000 | Rayate | 3 | 582347 | saving |
| 2 | Tm | 2002-11-11 | 20000 | Rahata | 3 | 582347 | saving |
| 3 | Gb | 2002-02-02 | 20001 | Gavandgaon | 3 | 582347 | saving |
| 4 | Ak | 2002-10-10 | 50000 | Dhule | 3 | 582347 | saving |
| 5 | Tg | 2002-12-02 | 30000 | Dhulgaon | 3 | 582347 | saving |
| 1 | Sm | 2002-10-02 | 12000 | Rayate | 4 | 53815 | saving |
| 2 | Tm | 2002-11-11 | 20000 | Rahata | 4 | 53815 | saving |
| 3 | Gb | 2002-02-02 | 20001 | Gavandgaon | 4 | 53815 | saving |
| 4 | Ak | 2002-10-10 | 50000 | Dhule | 4 | 53815 | saving |
| 5 | Tg | 2002-12-02 | 30000 | Dhulgaon | 4 | 53815 | saving |
| 1 | Sm | 2002-10-02 | 12000 | Rayate | 5 | 535858 | saving |
| 2 | Tm | 2002-11-11 | 20000 | Rahata | 5 | 535858 | saving |
| 3 | Gb | 2002-02-02 | 20001 | Gavandgaon | 5 | 535858 | saving |
| 4 | Ak | 2002-10-10 | 50000 | Dhule | 5 | 535858 | saving |
| 5 | Tg | 2002-12-02 | 30000 | Dhulgaon | 5 | 535858 | saving |
+--------+------+------------+--------+------------+--------+---------+----------+
25 rows in set (0.01 sec)
************************************************************************************************

CREATE VIEW:-
mysql> CREATE VIEW emp_view as select *from empl_tbl;
Query OK, 0 rows affected (0.03 sec)

mysql> select *from emp_view;


+--------+------+------------+--------+------------+
| emp_id | name | DOB | salary | Address |
+--------+------+------------+--------+------------+
| 1 | Sm | 2002-10-02 | 12000 | Rayate |
| 2 | Tm | 2002-11-11 | 20000 | Rahata |
| 3 | Gb | 2002-02-02 | 20001 | Gavandgaon |
| 4 | Ak | 2002-10-10 | 50000 | Dhule |
| 5 | Tg | 2002-12-02 | 30000 | Dhulgaon |
+--------+------+------------+--------+------------+
5 rows in set (0.00 sec)
************************************************************************************************

CREATE VIEW FOR SPECIFIC COLUMNS:-


mysql> create view emp_view1 as select emp_id, salary from empl_tbl;
Query OK, 0 rows affected (0.05 sec)

mysql> select *from emp_view1;


+--------+--------+
| emp_id | salary |
+--------+--------+
| 1 | 12000 |
| 2 | 20000 |
| 3 | 20001 |
| 4 | 50000 |
| 5 | 30000 |
+--------+--------+
5 rows in set (0.00 sec)

AVERAGE FROM VIEW TABLE-


mysql> select avg(salary) from emp_view;
+-------------+
| avg(salary) |
+-------------+
| 26400.2000 |
+-------------+
1 row in set (0.00 sec)

MIN
mysql> select min(salary) from emp_view;
+-------------+
| min(salary) |
+-------------+
| 12000 |
+-------------+
1 row in set (0.00 sec)

MAX
mysql> select max(salary) from emp_view;
+-------------+
| max(salary) |
+-------------+
| 50000 |
+-------------+
1 row in set (0.00 sec)

DROP VIEW
mysql> drop view emp_view;
Query OK, 0 rows affected (0.00 sec)

mysql> select *from emp_view;


ERROR 1136 (32SO2): Table ‘sndcoe.emp_view’ doesn’t exist.

mysql> exit;
Bye
unix@unix-V520-15IKL:~$

You might also like