0% found this document useful (0 votes)
28 views7 pages

Lab-4 & 5

The document contains SQL commands for creating and managing databases related to employees, students, and staff. It includes the creation of tables, insertion of records, and various SQL queries to retrieve and analyze data. Additionally, it demonstrates different types of joins between the Student and Staff tables.

Uploaded by

magimegala1105
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)
28 views7 pages

Lab-4 & 5

The document contains SQL commands for creating and managing databases related to employees, students, and staff. It includes the creation of tables, insertion of records, and various SQL queries to retrieve and analyze data. Additionally, it demonstrates different types of joins between the Student and Staff tables.

Uploaded by

magimegala1105
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/ 7

LAB-3 EXERCISES

CREATE DATABASE IF NOT EXISTS employee_db;

USE employee_db;

CREATE TABLE IF NOT EXISTS employees (


employee_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
department VARCHAR(50) NOT NULL,
age INT,
gender ENUM('Male', 'Female', 'Other'),
salary INT(10),
address VARCHAR(255)
);

INSERT INTO employees (name, department, age, gender, salary, address) VALUES
('John Smith', 'HR', 30, 'Male', 50000, '123 Main St, City, State, Zip'),
('Jane Doe', 'IT', 35, 'Female', 60000, '456 Elm St, City, State, Zip'),
('Michael Johnson', 'Sales', 28, 'Male', 55000, '789 Oak St, City, State, Zip'),
('Emily Brown', 'Marketing', 32, 'Female', 52000, '101 Pine St, City, State, Zip'),
('David Jones', 'Finance', 40, 'Male', 70000, '234 Cedar St, City, State, Zip'),
('Jessica Wilson', 'HR', 33, 'Female', 58000, '567 Maple St, City, State, Zip'),
('Daniel Lee', 'IT', 29, 'Male', 65000, '890 Birch St, City, State, Zip'),
('Jennifer Martinez', 'Sales', 31, 'Female', 60000, '111 Spruce St, City, State, Zip'),
('Christopher Taylor', 'Marketing', 36, 'Male', 62000, '222 Walnut St, City, State, Zip'),
('Sarah Anderson', 'Finance', 38, 'Female', 75000, '333 Pine St, City, State, Zip'),
('Robert Wilson', 'HR', 45, 'Male', 80000, '444 Oak St, City, State, Zip'),
('Emma Garcia', 'IT', 27, 'Female', 53000, '555 Elm St, City, State, Zip'),
('William Martinez', 'Sales', 34, 'Male', 67000, '666 Maple St, City, State, Zip'),
('Sophia Thomas', 'Marketing', 29, 'Female', 54000, '777 Cedar St, City, State, Zip'),
('James Brown', 'Finance', 42, 'Male', 72000, '888 Pine St, City, State, Zip');
SELECT * FROM employees;

SELECT * FROM employees WHERE department = 'HR';

SELECT * FROM employees WHERE salary > 60000;

SELECT * FROM employees WHERE age BETWEEN 30 AND 40;

SELECT * FROM employees WHERE gender = 'Male';

SELECT * FROM employees WHERE gender = 'Female' AND department = 'Finance';

SELECT * FROM employees WHERE name LIKE 'J%';

SELECT * FROM employees ORDER BY salary DESC;

SELECT AVG(salary) AS average_salary FROM employees;

SELECT department, COUNT(*) AS employee_count FROM employees GROUP BY


department;
LAB-4 EXERCISES
CREATE TABLE Std_Mark (
RegNo INT PRIMARY KEY,
Name VARCHAR(255),
Dept VARCHAR(255),
OS FLOAT,
SW FLOAT,
CG FLOAT,
IOT FLOAT
);

INSERT INTO Std_Mark (RegNo, Name, Dept, OS, SW, CG, IOT) VALUES
(101, 'John Doe', 'Computer Science', 85, 78, 90, 88),
(102, 'Jane Smith', 'Computer Science'', 70, 80, 75, 85),
(103, 'Alice Johnson', 'Information Technology', 90, 85, 88, 92),
(104, 'Bob Brown', 'Computer Science', 82, 75, 80, 85),
(105, 'Emma Lee', 'Computer Science', 88, 90, 85, 87),
(106, 'Michael Johnson', 'Computer Science', 75, 80, 70, 78),
(107, 'Sarah Williams', 'Computer Science', 85, 75, 78, 80),
(108, 'David Miller', 'Information Technology', 92, 88, 90, 85),
(109, 'Olivia Taylor', 'Computer Science', 78, 85, 80, 75),
(110, 'Ethan Martinez', 'Computer Science', 85, 90, 82, 88),
(111, 'Sophia Anderson', 'Computer Science', 90, 82, 85, 88),
(112, 'Liam Wilson', 'Computer Science', 80, 85, 75, 82),
(113, 'Ava Thompson', 'Information Technology', 88, 92, 85, 90),
(114, 'Noah Garcia', 'Computer Science', 82, 78, 85, 80),
(115, 'Isabella Hernandez', 'Computer Science', 90, 85, 88, 92);
SELECT COUNT(*) AS Total_Students FROM Std_Mark;

SELECT SUM(OS) AS Total_OS_Marks, SUM(SW) AS Total_SW_Marks, SUM(CG) AS


Total_CG_Marks, SUM(IOT) AS Total_IOT_Marks FROM Std_Mark;

SELECT AVG(OS) AS Average_OS_Marks, AVG(SW) AS Average_SW_Marks, AVG(CG)


AS Average_CG_Marks, AVG(IOT) AS Average_IOT_Marks FROM Std_Mark;

SELECT MIN(OS) AS Min_OS_Marks, MIN(SW) AS Min_SW_Marks, MIN(CG) AS


Min_CG_Marks, MIN(IOT) AS Min_IOT_Marks FROM Std_Mark;

SELECT MAX(OS) AS Max_OS_Marks, MAX(SW) AS Max_SW_Marks, MAX(CG) AS


Max_CG_Marks, MAX(IOT) AS Max_IOT_Marks FROM Std_Mark;

SELECT Dept, AVG(OS) AS Avg_OS, AVG(SW) AS Avg_SW, AVG(CG) AS Avg_CG,


AVG(IOT) AS Avg_IOT FROM Std_Mark GROUP BY Dept;
LAB-5 EXERCISES

-- Create Student table


CREATE TABLE Student (
StudentID INT PRIMARY KEY AUTO_INCREMENT,
Name VARCHAR(255),
Gender VARCHAR(10),
DateOfBirth DATE,
Dept VARCHAR(255)
);

-- Insert records into Student table


INSERT INTO Student (Name, Gender, DateOfBirth, Dept) VALUES
('John Doe', 'Male', '2000-05-15', 'Computer Science'),
('Jane Smith', 'Female', '1999-09-20', 'Electrical Engineering'),
('Alice Johnson', 'Female', '2001-02-10', 'Computer Science'),
('Bob Brown', 'Male', '2000-08-25', 'Electrical Engineering'),
('Emma Lee', 'Female', '2000-06-30', 'Software Engineering'),
('Michael Johnson', 'Male', '2001-01-05', 'Computer Science'),
('Sarah Williams', 'Female', '1999-11-18', 'Electrical Engineering'),
('David Miller', 'Male', '2000-04-02', 'Information Technology'),
('Olivia Taylor', 'Female', '2000-10-12', 'Computer Science'),
('Ethan Martinez', 'Male', '2001-03-22', 'Software Engineering'),
('Sophia Anderson', 'Female', '2000-07-08', 'Computer Science'),
('Liam Wilson', 'Male', '1999-12-13', 'Electrical Engineering'),
('Ava Thompson', 'Female', '2000-09-28', 'Information Technology'),
('Noah Garcia', 'Male', '2000-11-05', 'Computer Science'),
('Isabella Hernandez', 'Female', '2001-04-16', 'Software Engineering');
-- Create Staff table
CREATE TABLE Staff (
StaffID INT PRIMARY KEY AUTO_INCREMENT,
Name VARCHAR(255),
Gender VARCHAR(10),
DateOfBirth DATE,
Dept VARCHAR(255),
Position VARCHAR(255)
);

-- Insert records into Staff table


INSERT INTO Staff (Name, Gender, DateOfBirth, Dept, Position) VALUES
('George Anderson', 'Male', '1980-03-25', 'Computer Science', 'Professor'),
('Emily Davis', 'Female', '1975-11-12', 'Electrical Engineering', 'Professor'),
('Jacob Martinez', 'Male', '1982-07-30', 'Software Engineering', 'Professor'),
('Emma Johnson', 'Female', '1978-09-08', 'Computer Science', 'Associate Professor'),
('Daniel Brown', 'Male', '1983-01-15', 'Electrical Engineering', 'Assistant Professor'),
('Sophia Wilson', 'Female', '1981-05-20', 'Information Technology', 'Associate
Professor'),
('William Garcia', 'Male', '1979-04-18', 'Computer Science', 'Professor'),
('Olivia Thompson', 'Female', '1984-08-28', 'Software Engineering', 'Associate
Professor'),
('Alexander Lee', 'Male', '1980-12-05', 'Computer Science', 'Assistant Professor'),
('Mia Smith', 'Female', '1977-06-10', 'Electrical Engineering', 'Professor'),
('Ethan Williams', 'Male', '1982-03-03', 'Information Technology', 'Assistant Professor'),
('Ava Martin', 'Female', '1976-10-22', 'Computer Science', 'Professor'),
('Noah Taylor', 'Male', '1981-09-15', 'Software Engineering', 'Associate Professor'),
('Isabella Hernandez', 'Female', '1979-12-08', 'Computer Science', 'Professor'),
('James Davis', 'Male', '1985-02-20', 'Electrical Engineering', 'Associate Professor');
Inner Join: Returns only the rows where there is a match in both tables.

SELECT Student.*, Staff.*


FROM Student
INNER JOIN Staff ON Student.Dept = Staff.Dept;

Left Outer Join: Returns all rows from the left table (Student), and the matched rows from the
right table (Staff). If there is no match, NULL values are returned for the right table columns.

SELECT Student.*, Staff.*


FROM Student
LEFT OUTER JOIN Staff ON Student.Dept = Staff.Dept;

Right Outer Join: Returns all rows from the right table (Staff), and the matched rows from the
left table (Student). If there is no match, NULL values are returned for the left table columns.

SELECT Student.*, Staff.*


FROM Student
RIGHT OUTER JOIN Staff ON Student.Dept = Staff.Dept;

Full Outer Join (Not supported in MySQL directly, but can be simulated using UNION of Left
and Right Outer Joins):

SELECT Student.*, Staff.*


FROM Student
LEFT OUTER JOIN Staff ON Student.Dept = Staff.Dept
UNION
SELECT Student.*, Staff.*
FROM Student
RIGHT OUTER JOIN Staff ON Student.Dept = Staff.Dept;

Cross Join: Returns the Cartesian product of the two tables, i.e., all possible combinations of
rows from both tables.

SELECT Student.*, Staff.*


FROM Student
CROSS JOIN Staff;

You might also like