Lab Inter-2 QP Sets
Lab Inter-2 QP Sets
Lab Inter-2 QP Sets
Lab Internal-II
Set-1
A;
SELECT s.Name, s.Age, sc.CourseID
FROM Student s
JOIN Student_Course sc ON s.RollNo = sc.RollNo;
b)List all the students records who have enrolled in same courses.
A:
SELECT s1.Name, s2.Name, sc.CourseID
FROM Student_Course sc
JOIN Student s1 ON sc.RollNo = s1.RollNo
JOIN Student s2 ON sc.RollNo = s2.RollNo
WHERE s1.RollNo < s2.RollNo AND sc.CourseID IN (
SELECT CourseID
FROM Student_Course
GROUP BY CourseID
HAVING COUNT(DISTINCT RollNo) > 1
);
A:
SELECT AVG(Age) AS AverageAge
FROM Student;
A:
A:
SELECT sc.CourseID, s.Name
FROM Student_Course sc
JOIN Student s ON sc.RollNo = s.RollNo;
2. Suppose that your company has a business rule that no employee is allowed to have a salary greater than $200,000 unless the
authorizing manager’s employee ID has been entered with the transaction. Assume that a table, named employees, contains these
columns:
Column
Data type
name
EmployeeID Int
Salary Smallmoney
HireDate Smalldatetime
ApprovalID Int
A:
CREATE TRIGGER CheckSalary
BEFORE INSERT ON employees
FOR EACH ROW
BEGIN
DECLARE v_EmployeeID INT;
DECLARE v_Salary DECIMAL(10, 2);
DECLARE v_ApprovalID INT;
3.Bills Consider the following relational schema that manages the telephone bills of a mobile phone company.
CUSTOMER ( SSN, Name, Surname, PhoneNum, Plan)
PRICINGPLAN ( Code, ConnectionFee, PricePerSecond )
PHONECALL ( SSN, Date, Time, CalledNum, Seconds) BILL ( SSN, Month, Year, amount )
Write a trigger that after each phone call updates the customer's bill.
A:
CREATE TRIGGER UpdateBillAfterPhoneCall
AFTER INSERT ON PHONECALL
FOR EACH ROW
BEGIN
DECLARE v_ConnectionFee DECIMAL(10, 2);
DECLARE v_PricePerSecond DECIMAL(10, 2);
DECLARE v_Seconds INT;
DECLARE v_Amount DECIMAL(10, 2);
DECLARE v_Month INT;
DECLARE v_Year INT;
⦁Select the details of the employee who work either for department E-104 or E-102.
A:
mysql> select * from emp where department in("E-104","E-102");
+-------+---------+------------+------------+-----------------+-------------+
| empid | empname | department | contact | emailid | emailheadid |
+-------+---------+------------+------------+-----------------+-------------+
| 102 | Priya | E-104 | 1234567890 | [priya@yahoo.com](mailto:priya@yahoo.com) | 103 |
| 104 | Rahul | E-102 | 1234567890 | [rahul@yahoo.com](mailto:rahul@yahoo.com) | 105 |
+-------+---------+------------+------------+-----------------+-------------+
⦁select the name of the employee whose name's 3rd charactor is 'h'.
A:
mysql> SELECT empname
-> FROM emp
-> WHERE SUBSTRING(empname, 3, 1) = 'h';
+----------+
| empname |
+----------+
| Isha |
| Neha |
| Rahul |
| Abhishek |
+----------+
2.Update the missing data in the following table:
Lab Internal-II
Set-3
⦁List the names and addresses of all guests in 123 Main Street, City, alphabetically ordered by name.
A:
mysql> SELECT Name, Address
-> FROM Guest
-> WHERE Address = '123 Main Street, City'
-> ORDER BY Name;
Empty set (0.00 sec)
⦁List all double or family rooms with a price below £40.00 per night, in ascending order of price.
A:
mysql> SELECT *
-> FROM Room
-> WHERE Type IN ('Double', 'Family') AND Price < 40.00
-> ORDER BY Price ASC;
Empty set (0.00 sec)
2. Customers:
Field Type Null Key Default Extra
CustomerID varchar(5) NO PRI NULL
CompanyName varchar(40) NO MUL NULL
ContactName varchar(30) YES NULL
ContactTitle varchar(30) YES NULL
Address varchar(60) YES NULL
City varchar(15) YES MUL NULL
Region varchar(15) YES MUL NULL
PostalCode varchar(10) YES MUL NULL
Country varchar(15) YES NULL
Phone varchar(24) YES NULL
Fax varchar(24) YES NULL
a.Write an SQL query to display CustomerID, CompnayName of Customers whose City is ‘Berlin@ or
‘London’
b. Write an SQL query to select all CustomerID, CompnayName and City from the "Customers" table in
the DESCENDING order by the "Country" column
c. Write an SQL query to display the contactname,title and city from the Customers table where city
name starts with 'Lo'.
d. Write an SQL query to display the customerid, name and city from the Customers table where city
starts with p and ends with d.
e. Write an SQL query to lists the number of customers and countryname in each country in descending
order where the number of customers are more than 5
mysql> INSERT INTO Customers5 (CustomerID, CompanyName, ContactName, City, Country, Phone)
-> VALUES
-> ('C001', 'Berlin Company', 'Max Mustermann', 'Berlin', 'Germany', '123-456-7890');
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO Customers5 (CustomerID, CompanyName, ContactName, City, Country, Phone)
-> VALUES
-> ('C002', 'London Ltd.', 'John Smith', 'London', 'United Kingdom', '987-654-3210');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO Customers5 (CustomerID, CompanyName, ContactName, City, Country, Phone)
-> VALUES
-> ('C003', 'Paris Enterprises', 'Sophie Martin', 'Paris', 'France', '555-123-4567'),
-> ('C004', 'Rome Corporation', 'Luigi Rossi', 'Rome', 'Italy', '333-999-8888'),
-> ('C005', 'New York Industries', 'Emily Johnson', 'New York', 'USA', '111-222-3333');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
1.Write an SQL query to display CustomerID, CompnayName of Customers whose City is ‘Berlin@ or
‘London’
A:
mysql> SELECT CustomerID, CompanyName
-> FROM Customers5
-> WHERE City IN ('Berlin', 'London');
+------------+----------------+
| CustomerID | CompanyName |
+------------+----------------+
| C001 | Berlin Company |
| C002 | London Ltd. |
+------------+----------------+
2 rows in set (0.00 sec)
b. Write an SQL query to select all CustomerID, CompnayName and City from the "Customers" table in
the DESCENDING order by the "Country" column
A:
mysql> SELECT CustomerID, CompanyName, City
-> FROM Customers5
-> ORDER BY Country DESC;
+------------+---------------------+----------+
| CustomerID | CompanyName | City |
+------------+---------------------+----------+
| C005 | New York Industries | New York |
| C002 | London Ltd. | London |
| C004 | Rome Corporation | Rome |
| C001 | Berlin Company | Berlin |
| C003 | Paris Enterprises | Paris |
+------------+---------------------+----------+
5 rows in set (0.00 sec)
c. Write an SQL query to display the contactname,title and city from the Customers table where city
name starts with 'Lo'.
A:
mysql> SELECT ContactName, ContactTitle, City
-> FROM Customers5
-> WHERE City LIKE 'Lo%';
+-------------+--------------+--------+
| ContactName | ContactTitle | City |
+-------------+--------------+--------+
| John Smith | NULL | London |
+-------------+--------------+--------+
1 row in set (0.00 sec)
d. Write an SQL query to display the customerid, name and city from the Customers table where city
starts with p and ends with d.
A:
mysql> SELECT CustomerID, CompanyName, City
-> FROM Customers5
-> WHERE City LIKE 'p%d';
Empty set (0.00 sec)
e. Write an SQL query to lists the number of customers and countryname in each country in descending
order where the number of customers are more than 5
A:
mysql> SELECT COUNT(CustomerID) AS NumberOfCustomers, Country
-> FROM Customers5
-> GROUP BY Country
-> HAVING NumberOfCustomers > 5
-> ORDER BY NumberOfCustomers DESC;
Empty set (0.00 sec)
mysql> CREATE TABLE Employee2 (EMPNO INT PRIMARY KEY, ENAME VARCHAR(50), JOB VARCHAR(50),
MGR INT, HIREDATE DATE, SAL DECIMAL(10, 2), COMM DECIMAL(10, 2), DEPTNO INT, AGE INT, ESAL
DECIMAL(10, 2));
Query OK, 0 rows affected (0.01 sec)
mysql> INSERT INTO Employee2 (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, AGE,
ESAL)
-> VALUES
-> (101, 'John Doe', 'Manager', NULL, '2023-01-15', 50000.00, 15000.00, 10, 35, 65000.00),
-> (102, 'Jane Smith', 'Analyst', 101, '2023-02-20', 40000.00, NULL, 20, 28, 45000.00),
-> (103, 'Michael Johnson', 'Developer', 101, '2023-03-10', 35000.00, 12000.00, 10, 30, 40000.00),
-> (104, 'Emily Williams', 'Manager', NULL, '2023-04-05', 55000.00, 18000.00, 30, 38, 70000.00),
-> (105, 'Robert Brown', 'Designer', 104, '2023-05-25', 42000.00, NULL, 30, 29, 48000.00);
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0
### Trigger:
A:
mysql> DELIMITER //
mysql> CREATE TRIGGER CheckCommission
-> BEFORE INSERT ON Employee2
-> FOR EACH ROW
-> BEGIN
-> IF NEW.COMM > 10000 THEN
-> SIGNAL SQLSTATE '45000'
-> SET MESSAGE_TEXT = 'Commission above 10000 is not allowed';
-> END IF;
-> END;
-> //
Query OK, 0 rows affected (0.00 sec)
Department of Computer Science & Engineering
Lab Internal-II
Set-4
Creating table:
Inserting values:
INSERT INTO Employee (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, AGE, ESAL)
VALUES
(104, 'Emily Brown', 'Analyst', 102, '2022-01-10', 3200.00, 300.00, 20, 26, 3500.00),
(105, 'David Lee', 'Clerk', 103, '2023-04-05', 1800.00, NULL, 10, 23, 2000.00),
(106, 'Sarah Miller', 'Manager', NULL, '2019-08-25', 5200.00, NULL, 30, 40, 6000.00),
(107, 'Robert Wilson', 'Analyst', 106, '2020-11-12', 3500.00, 400.00, 30, 32, 4200.00),
(108, 'Linda Davis', 'Clerk', 106, '2023-02-18', 1900.00, NULL, 30, 22, 2100.00);
a. List all employee names and their salaries, whose salary lies between 1500/- and 3500/- both
inclusive.
A:
SELECT ENAME, SAL
FROM Employee
WHERE SAL >= 1500 AND SAL <= 3500;
A:
DESC Employee;
c. List all employee names and their and their manager whose manager is 7902 or 7566 0r 7789.
A:
SELECT E.ENAME AS EmployeeName,
M.ENAME AS ManagerName
FROM Employee E, Employee M
WHERE E.MGR = M.EMPNO
AND M.EMPNO IN (7902, 7566, 7789);
A:
SELECT *
FROM Employee
WHERE ENAME LIKE 'J%' OR ENAME LIKE 'T%';
e. .List all employee names and jobs, whose job title includes M or P.
A:
SELECT ENAME, JOB
FROM Employee
WHERE JOB LIKE '%M%' OR JOB LIKE '%P%';
Creating table:
Inserting values:
Procedure:
DELIMITER //
CREATE PROCEDURE DisplayEmployeeInfo()
BEGIN
SELECT name, occupation
FROM Employee
WHERE working_hours > 12;
END //
DELIMITER ;
Calling:
CALL DisplayEmployeeInfo();
Lab Internal-II
Set-5
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO AGE ESAL
Creating table:
CREATE TABLE Employee (
EMPNO INT PRIMARY KEY,
ENAME VARCHAR(50),
JOB VARCHAR(30),
MGR INT,
HIREDATE DATE,
SAL DECIMAL(10, 2),
COMM DECIMAL(10, 2),
DEPTNO INT,
AGE INT,
ESAL DECIMAL(10, 2)
);
Inserting values:
INSERT INTO Employee (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO,
AGE, ESAL)
VALUES
(1, 'John Doe', 'Manager', NULL, '2022-01-15', 6000.00, NULL, 10, 40, 7000.00),
(2, 'Jane Smith', 'Analyst', 1, '2022-03-20', 4000.00, 500.00, 20, 28, 4500.00),
(3, 'Mike Johnson', 'Clerk', 2, '2022-06-10', 2500.00, 100.00, 10, 22, 2700.00),
(4, 'Emily Brown', 'Developer', 1, '2021-11-05', 5500.00, 1000.00, 30, 32, 6500.00),
(5, 'David Lee', 'Manager', NULL, '2023-02-28', 7000.00, NULL, 20, 38, 8000.00);
A:
SELECT DISTINCT JOB FROM Employee;
+-----------+
| JOB |
+-----------+
| Manager |
| Analyst |
| Clerk |
| Developer |
+-----------+
A:
Output:
+-------+--------------+----------+------+------------+---------+---------+--------+-----+--------+
| EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO | AGE | ESAL |
+-------+--------------+----------+------+------------+---------+---------+--------+-----+--------+
+-------+--------------+----------+------+------------+---------+---------+--------+-----+--------+
Output:
+--------------+---------+------------+
+--------------+---------+------------+
+--------------+---------+------------+
A:
SELECT
MIN(SAL) AS MinSalary,
MAX(SAL) AS MaxSalary,
AVG(SAL) AS AvgSalary
FROM Employee;
Output:
+-----------+-----------+-----------+
+-----------+-----------+-----------+
+-----------+-----------+-----------+
• Find how many job titles are available in employee table.
A:
Output:
+-------------------+
| NumberOfJobTitles |
+-------------------+
|4 |
+-------------------+
2. Write a trigger to increment scount of sailorcount table by one for each insertion into sailors table.
A:
DELIMITER //
BEGIN
UPDATE sailorcount
END;
//
DELIMITER ;
A:
DELIMITER //
CREATE PROCEDURE DisplayEmployeeNames()
BEGIN
SELECT name FROM Employee;
END;
//
DELIMITER ;
Calling:
CALL DisplayEmployeeNames();
Lab Internal-II
Set-6
1.Create a trigger invoked automatically when insert the working_hours <0 on following structure of a
table.
Table(name NOT NULL, occupation NOT NULL, working_date ,working_hours).
A:
Creating table:
CREATE TABLE Employee (
name VARCHAR(50) NOT NULL,
occupation VARCHAR(50) NOT NULL,
working_date DATE,
working_hours DECIMAL(5, 2)
);
Inserting values:
Trigger:
DELIMITER //
CREATE TRIGGER CheckWorkingHours
BEFORE INSERT ON Employee
FOR EACH ROW
BEGIN
IF NEW.working_hours < 0 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Working hours cannot be negative';
END IF;
END;
//
DELIMITER ;
Calling:
CALL ValidateSailorFields();
Creating Tables:
Inserting data:
• Find the names and cities of residence of all employee who works of "First Bank Corporation".
A:
SELECT Emp.name, Emp.city
FROM Emp
JOIN works ON Emp.name = works.employee_name
WHERE works.company_name = 'First Bank Corporation';
Output:
+-----------+----------+
| name | city |
+------------+----------+
| John Doe | New York |
| Mike Johnson | Chicago |
+------------+----------+
• Find the names,street addresses and cities of residence of all employees who works for "First
Bank Corporation" and earns more than $10,000.
A:
SELECT Emp.name, Emp.street, Emp.city
FROM Emp
JOIN works ON Emp.name = works.employee_name
WHERE works.company_name = 'First Bank Corporation' AND works.salary > 10000.00;
Output:
+------------+-------------+----------+
| name | street | city |
+------------+-------------+----------+
| Mike Johnson | 789 Oak St | Chicago |
+------------+-------------+----------+
• Find all employees in the database who do not work for "First Bank Corporation”
A:
SELECT Emp.name
FROM Emp
WHERE Emp.name NOT IN (
SELECT DISTINCT works.employee_name
FROM works
WHERE works.company_name = 'First Bank Corporation'
);
Output:
+------------+
| name |
+------------+
| Jane Smith |
| Emily Brown |
| David Lee |
+------------+
• Find all employees in the database who earn more than each employee of "Small Bank
Corporation".
A:
SELECT Emp.name
FROM Emp
WHERE NOT EXISTS (
SELECT 1
FROM works
WHERE works.company_name = 'Small Bank Corporation'
AND works.salary >= Emp.salary
);
Output:
+------------+
| name |
+------------+
| John Doe |
| Mike Johnson |
| David Lee |
+------------+
• Find the company that has the most employees.
A:
SELECT company_name, COUNT(employee_name) AS employee_count
FROM works
GROUP BY company_name
ORDER BY employee_count DESC
LIMIT 1;
Output:
+--------------------+----------------+
| company_name | employee_count |
+--------------------+----------------+
| Second Tech Inc | 2 |
+--------------------+----------------+