Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

1.

To study and implement commands under Data Definition Language and


apply different types of constraints.

Construct an Employee table with attributes Emp name, Emp ID, Location,
Designation, Salary.

Alter the salary attribute to gross salary.

Rename the table employee to Infosys Employee

Apply primary Key Constraint in Emp ID


Ans:
CREATE TABLE Employee (
EmpID INT PRIMARY KEY,
EmpName VARCHAR(255) NOT NULL,
Location VARCHAR(100),
Designation VARCHAR(100),
Salary DECIMAL(10, 2)
);
ALTER TABLE Employee
RENAME COLUMN Salary TO GrossSalary;

ALTER TABLE Employee


RENAME TO "Infosys Employee";

ALTER TABLE "Infosys Employee"


ADD CONSTRAINT PK_EmpID PRIMARY KEY (EmpID);
2.

To study and implement commands under Data Manipulation Language.

Construct an Student table with attributes Student name, Student ID, Department,
Subject, Marks & minimum 5 rows.

Update the student name Shyam to Ram

Delete the student value where id = 4

Modify the department IT to Comp

Keep the table structure and remove all values from the table.
Ans:

CREATE TABLE Student (


StudentID INT PRIMARY KEY,
StudentName VARCHAR(255) NOT NULL,
Department VARCHAR(100),
Subject VARCHAR(100),
Marks INT
);

-- Inserting sample data


INSERT INTO Student (StudentID, StudentName, Department, Subject, Marks)
VALUES
(1, 'John', 'Engineering', 'Math', 90),
(2, 'Alice', 'Science', 'Physics', 85),
(3, 'Bob', 'Arts', 'History', 75),
(4, 'Shyam', 'IT', 'Programming', 88),
(5, 'Eve', 'Business', 'Economics', 78);

UPDATE Student
SET StudentName = 'Ram'
WHERE StudentName = 'Shyam';

DELETE FROM Student


WHERE StudentID = 4;

UPDATE Student
SET Department = 'Comp'
WHERE StudentID = 1;

DELETE FROM Student;


3.To study and implement Set Operations
Apply set operators on two tables i.e student1 table ( name roll no) and student2
table ( name roll no)

Ans:
-- Create the student1 table
CREATE TABLE student1 (
name VARCHAR(255),
roll_no INT
);

-- Create the student2 table


CREATE TABLE student2 (
name VARCHAR(255),
roll_no INT
);

-- Insert data into student1


INSERT INTO student1 (name, roll_no)
VALUES
('John', 101),
('Alice', 102),
('Bob', 103),
('Eve', 104);

-- Insert data into student2


INSERT INTO student2 (name, roll_no)
VALUES
('Alice', 102),
('Shyam', 105),
('Charlie', 106);

-- UNION
SELECT name, roll_no FROM student1
UNION
SELECT name, roll_no FROM student2;

-- INTERSECT
SELECT name, roll_no FROM student1
INTERSECT
SELECT name, roll_no FROM student2;

-- EXCEPT
SELECT name, roll_no FROM student1
EXCEPT
SELECT name, roll_no FROM student2;
4. To study and implement string function and constraints
Construct a Doctor table with columns doctor name, id, specialization.
Match patterns for Finds any values that starts with "a"
Match patterns for Finds any values that ends with "a"
Finds any values that have "r" in the second position
Apply primary key constraint in a column
Apply not null constraint

Ans:

CREATE TABLE Doctor (


id INT PRIMARY KEY,
doctor_name VARCHAR(255) NOT NULL,
specialization VARCHAR(100)
);

INSERT INTO Doctor (id, doctor_name, specialization) VALUES(1, 'Dr. Amanda', 'Cardiology');

SELECT doctor_name
FROM Doctor
WHERE doctor_name LIKE 'a%';

SELECT doctor_name
FROM Doctor
WHERE doctor_name LIKE '%a';

SELECT doctor_name
FROM Doctor
WHERE SUBSTRING(doctor_name, 2, 1) = 'r';
5. To study and implement Operators

Construct a student table with columns name, id, subject, marks, dept. and apply
special operators to show marks in between range of 20 to 30.
Display all names of students who have not got marks in range of 15 to 30.

Display all students who id is greater than 5

Display all students whose department is IT

Ans:

CREATE TABLE Student (


id INT PRIMARY KEY,
name VARCHAR(255),
subject VARCHAR(100),
marks INT,
dept VARCHAR(100)
);

INSERT INTO Student (id, name, subject, marks, dept) VALUES(1, 'John', 'Math', 25, 'Science');

SELECT name, marks


FROM Student
WHERE marks BETWEEN 20 AND 30;

SELECT name
FROM Student
WHERE marks < 15 OR marks > 30;

SELECT *
FROM Student
WHERE id > 5;

SELECT *
FROM Student
WHERE dept = 'IT';
6. To implement Join Queries.
Apply Right outer join, full outer join and left outer join on any table of your
choice.
Ans:
-- Create the Customers table
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(255),
ContactName VARCHAR(255)
);

-- Insert sample data into the Customers table


INSERT INTO Customers (CustomerID, CustomerName, ContactName)
VALUES
(1, 'Customer A', 'John'),
(2, 'Customer B', 'Alice'),
(3, 'Customer C', 'Bob');

-- Create the Orders table


CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerID INT,
OrderDate DATE
);

-- Insert sample data into the Orders table


INSERT INTO Orders (OrderID, CustomerID, OrderDate)
VALUES
(101, 1, '2023-01-10'),
(102, 1, '2023-02-15'),
(103, 2, '2023-01-20'),
(104, 3, '2023-03-05');

--Right Outer Join.


SELECT Customers.CustomerName, Orders.OrderDate
FROM Customers
RIGHT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

--Full Outer Join.


SELECT Customers.CustomerName, Orders.OrderDate
FROM Customers
FULL JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

--Left Outer Join.


SELECT Customers.CustomerName, Orders.OrderDate
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
7. To Implement Basic and Nested complex SQL queries.
Ans:
-- Create the Students table
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
StudentName VARCHAR(255),
Age INT,
Department VARCHAR(100)
);

-- Insert sample data into the Students table


INSERT INTO Students (StudentID, StudentName, Age, Department)
VALUES
(1, 'John', 20, 'Science'),
(2, 'Alice', 22, 'Arts'),
(3, 'Bob', 21, 'Engineering'),
(4, 'Eve', 19, 'Science');

-- Create the Courses table


CREATE TABLE Courses (
CourseID INT PRIMARY KEY,
CourseName VARCHAR(255),
Credits INT
);

-- Insert sample data into the Courses table


INSERT INTO Courses (CourseID, CourseName, Credits)
VALUES
(101, 'Math', 4),
(102, 'History', 3),
(103, 'Physics', 4),
(104, 'Literature', 3);

Basic Query: Select all students from the Science department:

SELECT StudentName
FROM Students
WHERE Department = 'Science';

Basic Query: Count the number of students in each department:

SELECT Department, COUNT(*) AS TotalStudents


FROM Students
GROUP BY Department;

Nested Complex Query: Find the students who are taking courses with more than 3 credits:

SELECT StudentName
FROM Students
WHERE StudentID IN (
SELECT DISTINCT S.StudentID
FROM Students S
INNER JOIN Enrollments E ON S.StudentID = E.StudentID
INNER JOIN Courses C ON E.CourseID = C.CourseID
WHERE C.Credits > 3
);

8.

Implementation of different types of Aggregation Functions.


Display the count of car with color grey
display the minimum year
display the maximum year
Display the total cost of all cars
Display the average of cost of all car

CarMake Model Year Color cost


Toyota Camry XLE 2005 Grey 1000000
Honda Accord EX 2002 Black 2000000
Lexus ES 350 2008 Grey 1800000
BMW 3 Series Coupe 2008 Red 10000000
CREATE TABLE Car (
CarMake VARCHAR(255),
Model VARCHAR(255),
Year INT,
Color VARCHAR(255),
Cost DECIMAL(10, 2)
);

-- Insert the sample data


INSERT INTO Car (CarMake, Model, Year, Color, Cost)
VALUES
('Toyota', 'Camry XLE', 2005, 'Grey', 1000000),
('Honda', 'Accord EX', 2002, 'Black', 2000000),
('Lexus', 'ES 350', 2008, 'Grey', 1800000),
('BMW', '3 Series Coupe', 2008, 'Red', 10000000);

Count of cars with the color "Grey":

SELECT COUNT(*) AS GreyCarCount


FROM Car
WHERE Color = 'Grey';

Minimum year of the cars:

SELECT MIN(Year) AS MinYear


FROM Car;

Maximum year of the cars:


SELECT MAX(Year) AS MaxYear
FROM Car;

Total cost of all cars:

SELECT SUM(Cost) AS TotalCost


FROM Car;

Average cost of all cars:

SELECT AVG(Cost) AS AvgCost


FROM Car;
9.

Group by, Order by & Having Clause.


Construct table Student ( Student_Id Name , Marks , Subject)
Display COUNT of Marks whose marks are greater than 10.
Display names of student group by subject having marks greater than or equal to10

Ans:

CREATE TABLE Student (


Student_Id INT PRIMARY KEY,
Name VARCHAR(255),
Marks INT,
Subject VARCHAR(100)
);

-- Insert sample data into the Student table


INSERT INTO Student (Student_Id, Name, Marks, Subject)
VALUES
(1, 'John', 15, 'Math'),
(2, 'Alice', 8, 'Science'),
(3, 'Bob', 12, 'Math'),
(4, 'Eve', 9, 'Science'),
(5, 'Charlie', 14, 'Math');
SELECT COUNT(*) AS CountOfMarks
FROM Student
WHERE Marks > 10;

SELECT Subject, Name


FROM Student
WHERE Marks >= 10
GROUP BY Subject, Name
ORDER BY Subject;
10.Implementation of Views
Construct a student table and show marks of any 3 subjects.
Create a view for the same table having a view of any 2 subjects.
Update marks in view table
Ans:
CREATE TABLE Student (
StudentID INT PRIMARY KEY,
StudentName VARCHAR(255),
Subject VARCHAR(100),
Marks INT
);

-- Insert sample data into the Student table


INSERT INTO Student (StudentID, StudentName, Subject, Marks)
VALUES
(1, 'John', 'Math', 85),
(2, 'Alice', 'Science', 92),
(3, 'Bob', 'History', 78),
(4, 'Eve', 'English', 89),
(5, 'Charlie', 'Math', 91);

--For disply marks of any three sub.


SELECT StudentName, Subject, Marks
FROM Student
WHERE Subject IN ('Math', 'Science', 'History');

-- Create a view for the same table having a view of any 2 subjects
CREATE VIEW StudentMarksView AS
SELECT StudentName, Subject, Marks
FROM Student
WHERE Subject IN ('Math', 'Science');

--for update marks in view table


UPDATE StudentMarksView
SET Marks = 95
WHERE StudentName = 'John' AND Subject = 'Math';
11. Implementation of Triggers.
construct a trigger to display salary changes in the below table

+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ram | 32 | Ahmedabad | 2000.00 |
| 2 | shyam | 25 | Delhi | 1500.00 |
| 3 | raj | 23 | Kota | 2000.00 |
| 4 | priya | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
+----+----------+-----+-----------+----------+
Ans:

--this that table which are present in question.


CREATE TABLE Employee (
ID INT PRIMARY KEY,
NAME VARCHAR(255),
AGE INT,
ADDRESS VARCHAR(255),
SALARY DECIMAL(10, 2)
);

-- Insert sample data into the Employee table


INSERT INTO Employee (ID, NAME, AGE, ADDRESS, SALARY)
VALUES
(1, 'Ram', 32, 'Ahmedabad', 2000.00),
(2, 'Shyam', 25, 'Delhi', 1500.00),
(3, 'Raj', 23, 'Kota', 2000.00),
(4, 'Priya', 25, 'Mumbai', 6500.00),
(5, 'Hardik', 27, 'Bhopal', 8500.00),
(6, 'Komal', 22, 'MP', 4500.00);

--create table to track salary changes


CREATE TABLE SalaryChangeLog (
ID INT PRIMARY KEY,
EmployeeID INT,
PreviousSalary DECIMAL(10, 2),
NewSalary DECIMAL(10, 2),
ChangeDate DATETIME
);
--creating trigger to display salary changes
DELIMITER //
CREATE TRIGGER LogSalaryChange
AFTER UPDATE ON Employee
FOR EACH ROW
BEGIN
IF OLD.Salary <> NEW.Salary THEN
INSERT INTO SalaryChangeLog (EmployeeID, PreviousSalary,
NewSalary, ChangeDate)
VALUES (NEW.ID, OLD.Salary, NEW.Salary, NOW());
END IF;
END;
//
DELIMITER ;

--update the employee table

UPDATE Employee
SET Salary = 2500.00
WHERE ID = 1;
12. To Study & implements TCL
Construct a student table and create a savepoint after insertion of 3 rows.
Ans:
CREATE TABLE Student (
StudentID INT PRIMARY KEY,
StudentName VARCHAR(255),
Age INT,
Department VARCHAR(100)
);

-- Insert sample data into the Student table


INSERT INTO Student (StudentID, StudentName, Age, Department)
VALUES
(1, 'John', 20, 'Science'),
(2, 'Alice', 22, 'Arts'),
(3, 'Bob', 21, 'Engineering'),
(4, 'Eve', 19, 'Science'),
(5, 'Charlie', 23, 'Math');

-- Start a transaction
START TRANSACTION;

-- Insert the first 3 rows


INSERT INTO Student (StudentID, StudentName, Age, Department)
VALUES
(6, 'David', 24, 'Engineering'),
(7, 'Fiona', 20, 'Math'),
(8, 'George', 22, 'Science');

-- Create a savepoint named "insert_savepoint" after inserting 3 rows


SAVEPOINT insert_savepoint;

-- Insert more rows (e.g., row 9 and 10)


INSERT INTO Student (StudentID, StudentName, Age, Department)
VALUES
(9, 'Helen', 23, 'Arts'),
(10, 'Ivan', 21, 'Math');

-- Commit the transaction


COMMIT;
13.
Implement functions in SQL.

Write a Function that computes and returns the maximum of two values.

Ans:
-- Create a function that computes and returns the maximum of two values
CREATE FUNCTION GetMaxValue (a INT, b INT)
RETURNS INT
BEGIN
DECLARE maxVal INT;

IF a >= b THEN
SET maxVal = a;
ELSE
SET maxVal = b;
END IF;

RETURN maxVal;
END;

-- Example of calling the function to get the maximum of two values


SELECT GetMaxValue(15, 25) AS MaximumValue;
14. Implement procedures in SQL.
Write a program finds the minimum of two values

Ans:
-- Create a procedure to find the minimum of two values
DELIMITER //
CREATE PROCEDURE FindMinimumValue(IN a INT, IN b INT, OUT minVal
INT)
BEGIN
IF a <= b THEN
SET minVal = a;
ELSE
SET minVal = b;
END IF;
END;
//
DELIMITER ;

-- Example of calling the procedure to find the minimum of two values


CALL FindMinimumValue(15, 25, @MinimumValue);
SELECT @MinimumValue AS MinimumValue;s

You might also like