Experiment 4
Experiment 4
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);
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');
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;
+-------+-----------+-------------+-----------+------+-------+
| 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;
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
Q7. Define a view NPlayer for obtaining the names of the countries & number of
players from each country.
+---------+----+
| Country | C |
+---------+----+
| India | 11 |
+---------+----+
1 row in set (0.00 sec)
(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
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);
Note: We can also write JOIN instead of INNER JOIN. JOIN is same as INNER
JOIN.
+-----------+----------+------+
| 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;
Note: We can also use LEFT OUTER JOIN instead of LEFT JOIN, both are same.
+----------+-----------+
| 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;
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.
+----------+-----------+
| 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;
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)