0% found this document useful (0 votes)
2 views8 pages

Experiment 4

The document outlines SQL operations including inserting new players, creating views for batsmen and bowlers, and querying player statistics. It also explains different types of SQL JOINs: INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN, with examples of their usage. Additionally, it includes creating and populating tables for students and courses, demonstrating how to retrieve data using various JOIN techniques.

Uploaded by

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

Experiment 4

The document outlines SQL operations including inserting new players, creating views for batsmen and bowlers, and querying player statistics. It also explains different types of SQL JOINs: INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN, with examples of their usage. Additionally, it includes creating and populating tables for students and courses, demonstrating how to retrieve data using various JOIN techniques.

Uploaded by

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

Experiment 4

INSERT, VIEWS & Different Types of SQL JOINs

Q1. Insert new player Michale Clark with player ID 300311 in the table player
mysql> INSERT INTO Player VALUES (300311,'Clark','Michale',NULL,NULL,NULL,NULL);

mysql> SELECT * FROM Player;

Q2. Insert new player Sam from India , Ahemdabad with player ID 406317 in the table
player
mysql> INSERT INTO Player (PlayerID, Bplace, Fname, Country) VALUES (406317, 'Ahemdabad', 'Sam',
'India');

mysql> SELECT * FROM Player;

Q3.
mysql> CREATE VIEW Batsman (PID,FName,LName,Country,MID,Score)
AS SELECT PlayerID,FName,LName,Country,MatchID,NRuns
FROM Player,Batting
WHERE PlayerID=PID;

mysql> describe Batsman;


+---------+-------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------------+------+-----+---------+-------+
| PID | int unsigned | NO | | NULL | |
| FName | varchar(15) | YES | | NULL | |
| LName | varchar(15) | YES | | NULL | |
| Country | varchar(25) | YES | | NULL | |
| MID | int unsigned | YES | | NULL | |
| Score | smallint unsigned | YES | | NULL | |
+---------+-------------------+------+-----+---------+-------+
6 rows in set (0.01 sec)

mysql> select * from Batsman;

+-------+-----------+-------------+-----------+------+-------+
| PID | FName | LName | Country | MID | Score |
+-------+-----------+-------------+-----------+------+-------+
| 23001 | Yuvraj | Singh | India | 2755 | 0 |
| 23001 | Yuvraj | Singh | India | 2689 | 38 |
| 24001 | Andrew | Symonds | Australia | 2689 | 42 |
| 25001 | MS | Dhoni | India | 2755 | 71 |
| 25001 | MS | Dhoni | India | 2689 | 36 |
| 27001 | Praveen | Kumar | India | 2689 | 7 |
| 27001 | Praveen | Kumar | India | 2755 | 2 |
| 89001 | Sachin | Tendulkar | India | 2689 | 91 |
| 91001 | Sanath | Jayasuriya | Sri Lanka | 2755 | 60 |
| 92002 | Muthiah | Murlitharan | Sri Lanka | 2755 | 1 |
| 94002 | Chaminda | Vaas | Sri Lanka | 2755 | 17 |
| 95001 | Ricky | Ponting | Australia | 2689 | 1 |
| 98002 | Harbhajan | Singh | India | 2689 | 3 |
| 98002 | Harbhajan | Singh | India | 2755 | 2 |
| 99001 | Breet | Lee | Australia | 2689 | 7 |
| 99002 | Adam | Gilchrist | Australia | 2689 | 2 |
+-------+-----------+-------------+-----------+------+-------+
16 rows in set (0.01 sec)
Q4.
mysql> CREATE VIEW Bowling2689 (PID,FName,LName,Country,NOvers,NWickets)
AS SELECT PlayerID,FName,LName,Country,NOvers,NWickets
FROM Player,Bowling
WHERE PlayerID=PID
AND MatchID=2689;

mysql> describe Bowling2689;


+----------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+-------+
| PID | int unsigned | NO | | NULL | |
| FName | varchar(15) | YES | | NULL | |
| LName | varchar(15) | YES | | NULL | |
| Country | varchar(25) | YES | | NULL | |
| NOvers | tinyint unsigned | YES | | NULL | |
| NWickets | tinyint unsigned | YES | | NULL | |
+----------+------------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

mysql> select * from Bowling2689;


+-------+-----------+---------+-----------+--------+----------+
| PID | FName | LName | Country | NOvers | NWickets |
+-------+-----------+---------+-----------+--------+----------+
| 99001 | Breet | Lee | Australia | 10 | 1 |
| 24001 | Andrew | Symonds | Australia | 3 | 1 |
| 23001 | Yuvraj | Singh | India | 3 | 0 |
| 98002 | Harbhajan | Singh | India | 10 | 1 |
+-------+-----------+---------+-----------+--------+----------+
4 rows in set (0.02 sec)

Q5. Find name of all players who have scored 60 or more runs.
Using view Batsman
mysql> SELECT FName, LName FROM Batsman WHERE Score >= 60;
+--------+------------+
| FName | LName |
+--------+------------+
| MS | Dhoni |
| Sanath | Jayasuriya |
| Sachin | Tendulkar |
+--------+------------+
3 rows in set (0.01 sec)
Without using view
mysql> SELECT FName, LName FROM Player,Batting WHERE PlayerID=PID AND NRuns>=60;
+--------+------------+
| FName | LName |
+--------+------------+
| MS | Dhoni |
| Sanath | Jayasuriya |
| Sachin | Tendulkar |
+--------+------------+
3 rows in set (0.00 sec)
Q6. Find the names of bowlers that have not taken any wickets in ODI match 2689
Using view Bowling2689

mysql> SELECT FName, LName FROM Bowling2689 WHERE NWickets=0;


+--------+-------+
| FName | LName |
+--------+-------+
| Yuvraj | Singh |
+--------+-------+
1 row in set (0.00 sec)

Without using view


mysql> SELECT FName, LName FROM Player,Bowling WHERE PlayerID=PID AND
MatchID=2689 AND NWickets=0;
+--------+-------+
| FName | LName |
+--------+-------+
| Yuvraj | Singh |
+--------+-------+
1 row in set (0.00 sec)

Q7. Define a view NPlayer for obtaining the names of the countries & number of
players from each country.

mysql> CREATE VIEW NPlayer (Country, C)


AS SELECT Country, COUNT(*)
FROM Player
GROUP BY Country;

mysql> SELECT Country, COUNT(*) AS C


FROM Player
GROUP BY Country
HAVING COUNT(*)=
(SELECT MAX(C) FROM NPlayer);

+---------+----+
| Country | C |
+---------+----+
| India | 11 |
+---------+----+
1 row in set (0.00 sec)

Display all tables and views.


mysql> SHOW FULL TABLES;
Create New Database for SQL JOINs
Different Types of SQL JOINs
Here are the different types of the JOINs in SQL:

(INNER) JOIN: Returns records that have matching values in both tables
LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the right table
RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the left table
FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table

CREATE TABLE Student (


Roll_No SMALLINT UNSIGNED,
Name varchar (20),
Address varchar (100),
Phone char(13),
Age TINYINT UNSIGNED);

insert into Student (Roll_No, Name, Address, Phone,Age) values (1,'Harsh','Delhi', '+919999999990', 18);
insert into Student (Roll_No, Name, Address, Phone,Age) values (2,'Pratik','Mumbai', '+919999999991', 19);
insert into Student values (3,'Priyanka','Pune', '+919999999992', 20),
(4,'Deep','Ahemdabad', '+919999999993', 18),
(5,'Sagar','Bhopal', '+919999999994', 19),
(6,'Raj','Haydrabad', '+919999999995', 20),
(7,'Rohit','Banglore', '+919999999996', 18),
(8,'Niraj','Nasik', '+919999999997', 19);

mysql> select * from Student;


+---------+----------+-----------+---------------+------+
| Roll_No | Name | Address | Phone | Age |
+---------+----------+-----------+---------------+------+
| 1 | Harsh | Delhi | +919999999990 | 18 |
| 2 | Pratik | Mumbai | +919999999991 | 19 |
| 3 | Priyanka | Pune | +919999999992 | 20 |
| 4 | Deep | Ahemdabad | +919999999993 | 18 |
| 5 | Sagar | Bhopal | +919999999994 | 19 |
| 6 | Raj | Haydrabad | +919999999995 | 20 |
| 7 | Rohit | Banglore | +919999999996 | 18 |
| 8 | Niraj | Nasik | +919999999997 | 19 |
+---------+----------+-----------+---------------+------+
8 rows in set (0.00 sec)

CREATE TABLE Course (


Course_ID SMALLINT UNSIGNED,
Roll_No SMALLINT UNSIGNED);

insert into Course(Course_Id,Roll_No) values (1,1);


insert into Course(Course_Id,Roll_No) values (2,2);
insert into Course values (2,3),(3,4),(1,5),(4,9),(5,10),(4,11);

mysql> select * from Course;


+-----------+---------+
| Course_ID | Roll_No |
+-----------+---------+
| 1 | 1 |
| 2 | 2 |
| 2 | 3 |
| 3 | 4 |
| 1 | 5 |
| 4 | 9 |
| 5 | 10 |
| 4 | 11 |
+-----------+---------+
8 rows in set (0.00 sec)
INNER JOIN: The INNER JOIN keyword selects all rows from both the tables as
long as the condition satisfies. This keyword will create the result-set by
combining all rows from both the tables where the condition satisfies i.e value
of the common field will be same.
Syntax:
SELECT tableA.column1,table1.column2,tableB.column1,....
FROM tableA
INNER JOIN tableB
ON tableA.matching_column = tableB.matching_column;

tableA: First table.

tableB: Second table

matching_column: Select all records from Table A and Table B,

where the join condition is met.

Note: We can also write JOIN instead of INNER JOIN. JOIN is same as INNER
JOIN.

mysql> SELECT Course.Course_ID, Student.Name, Student.Age FROM Student INNER


JOIN Course ON Student.Roll_NO = Course.Roll_NO;

+-----------+----------+------+
| Course_ID | Name | Age |
+-----------+----------+------+
| 1 | Harsh | 18 |
| 2 | Pratik | 19 |
| 2 | Priyanka | 20 |
| 3 | Deep | 18 |
| 1 | Sagar | 19 |
+-----------+----------+------+
5 rows in set (0.00 sec)
LEFT JOIN: This join returns all the rows of the table on the left side of the join
and matching rows for the table on the right side of join. The rows for which
there is no matching row on right side, the result-set will contain null. LEFT JOIN
is also known as LEFT OUTER JOIN.

Syntax:
SELECT tableA.column1,table1.column2,tableB.column1,....
FROM tableA
LEFT JOIN tableB
ON tableA.matching_column = tableB.matching_column;

tableA: First table.

tableB: Second table


matching_column: Select all records from Table A, along with
records from Table B for which the join condition is met (if at all).

Note: We can also use LEFT OUTER JOIN instead of LEFT JOIN, both are same.

mysql> SELECT Student.Name,Course.Course_ID FROM Student LEFT JOIN Course ON


Course.Roll_No = Student.Roll_No;

+----------+-----------+
| Name | Course_ID |
+----------+-----------+
| Harsh | 1 |
| Pratik | 2 |
| Priyanka | 2 |
| Deep | 3 |
| Sagar | 1 |
| Raj | NULL |
| Rohit | NULL |
| Niraj | NULL |
+----------+-----------+
8 rows in set (0.00 sec)
RIGHT JOIN: RIGHT JOIN is similar to LEFT JOIN. This join returns all the rows of
the table on the right side of the join and matching rows for the table on the
left side of join. The rows for which there is no matching row on left side, the
result-set will contain null. RIGHT JOIN is also known as RIGHT OUTER JOIN.

Syntax:
SELECT tableA.column1,table1.column2,tableB.column1,....
FROM tableA
RIGHT JOIN tableB
ON tableA.matching_column = tableB.matching_column;

tableA: First table.

tableB: Second table

matching_column: Select all records from Table B, along with records from
Table A for which the join condition is met (if at all).

Note: We can also use RIGHT OUTER JOIN instead of RIGHT JOIN, both are
same.

mysql> SELECT Student.Name,Course.Course_ID FROM Student RIGHT JOIN Course ON


Course.Roll_No = Student.Roll_No;

+----------+-----------+
| Name | Course_ID |
+----------+-----------+
| Harsh | 1 |
| Pratik | 2 |
| Priyanka | 2 |
| Deep | 3 |
| Sagar | 1 |
| NULL | 4 |
| NULL | 5 |
| NULL | 4 |
+----------+-----------+
8 rows in set (0.00 sec)
FULL JOIN: FULL JOIN creates the result-set by combining result of both LEFT JOIN
and RIGHT JOIN. The result-set will contain all the rows from both the tables. The rows
for which there is no matching, the result-set will contain NULL values.
Syntax:
SELECT tableA.column1,table1.column2,tableB.column1,....
FROM tableA FULL JOIN tableB
ON tableA.matching_column = tableB.matching_column;

tableA: First table.

tableB: Second table

matching_column: Select all records from Table A and Table B, regardless


of whether the join condition is met or not.

SELECT Student.Name,Course.Course_ID FROM Course FULL JOIN Student ON


Course.Roll_No = Student.Roll_No;

Note: If your Database does not support FULL JOIN (MySQL does not support
FULL JOIN), then you can use UNION ALL clause to combine these two JOINS as
shown below.
mysql> SELECT Student.Name,Course.Course_ID FROM Student RIGHT JOIN Course ON
Course.Roll_No = Student.Roll_No
UNION
SELECT Student.Name,Course.Course_ID FROM Student LEFT JOIN Course ON
Course.Roll_No = Student.Roll_No;
+----------+-----------+
| Name | Course_ID |
+----------+-----------+
| Harsh | 1 |
| Pratik | 2 |
| Priyanka | 2 |
| Deep | 3 |
| Sagar | 1 |
| NULL | 4 |
| NULL | 5 |
| Raj | NULL |
| Rohit | NULL |
| Niraj | NULL |
+----------+-----------+
10 rows in set (0.00 sec)

You might also like