1) Create the following table and perform the necessary commands given below : (a) Create Table Student with following attributes: 1.) Roll_No 2.) Name 3.) Date_of_Birth 4.) Branch 5.) Semester 6.) Address 7.) Year_of _Admission CREATE TABLE Student ( Roll_No INT PRIMARY KEY, Name VARCHAR(50) NOT NULL, Date_of_Birth DATE NOT NULL, Branch VARCHAR(50) NOT NULL, Semester INT NOT NULL, Address VARCHAR(100) NOT NULL, Year_of_Admission INT NOT NULL ); (b) Enter at least 10 records in the above table and answer the following queries using SQL : INSERT INTO Student (Roll_No, Name, Date_of_Birth, Branch, Semester, Address, Year_of_Admission) VALUES (1, 'John Doe', '2000-05-12', 'EXTC', 5, '123 Main St, Mumbai', 2018), (2, 'Jane Smith', '2001-08-18', 'Computer', 3, '456 Elm St, Mumbai', 2019), (3, 'Bob Johnson', '1999-02-04', 'EXTC', 6, '789 Maple Ave, Pune', 2017), (4, 'Sara Patel', '2002-01-01', 'Computer', 4, '321 Oak Rd, Pune', 2018), (5, 'David Lee', '2000-12-25', 'EXTC', 5, '987 High St, Mumbai', 2018), (6, 'Karen Chen', '2003-03-15', 'Computer', 2, '654 Pine St, Mumbai', 2020), (7, 'Mike Brown', '1998-07-29', 'EXTC', 7, '321 Oak Rd, Pune', 2016), (8, 'Lisa Gupta', '2001-06-07', 'EXTC', 4, '123 Main St, Pune', 2019), (9, 'Alex Singh', '2000-09-03', 'Computer', 6, '456 Elm St, Pune', 2017), (10, 'Priya Mehta', '1999-04-22', 'EXTC', 8, '789 Maple Ave, Mumbai', 2016); i.) Find the name of all the students who are enrolled in EXTC branch and having date of birth as 01/01/2002 SELECT Name FROM Student WHERE Branch = 'EXTC' AND Date_of_Birth = '2002-01-01'; +-------------+ | Name | +-------------+ | Sara Patel | +-------------+ ii.) List the name and roll number of all the students who are enrolled in year 2012. SELECT Roll_No, Name FROM Student WHERE Year_of_Admission = 2012; +---------+--------------+ | Roll_No | Name | +---------+--------------+ | 7 | Mike Brown | | 10 | Priya Mehta | +---------+--------------+ iii.) List the name and address of all the students enrolled in Computer Branch SELECT Name, Address FROM Student WHERE Branch = 'Computer'; +--------------+-----------------------+ | Name | Address | +--------------+-----------------------+ | Jane Smith | 456 Elm St, Mumbai | | Sara Patel | 321 Oak Rd, Pune | | Karen Chen | 654 Pine St, Mumbai | | Alex Singh | 456 Elm St, Pune | +--------------+-----------------------+ 2) Create the following table and perform the necessary task defined below : (a) Create the following table named DEPARTMENT (i) Dep_ID (ii) Name (iii) Year_Established (iv) Department_Head_Name (v) Address (vi) Telephone (vii) No_of_Employee CREATE TABLE DEPARTMENT ( Dep_ID INT PRIMARY KEY, Name VARCHAR(50) NOT NULL, Year_Established INT NOT NULL, Department_Head_Name VARCHAR(50) NOT NULL, Address VARCHAR(100) NOT NULL, Telephone VARCHAR(20) NOT NULL, No_of_Employee INT NOT NULL ); (b) Enter at least 10 records in the above table and answer the following queries using SQL : INSERT INTO DEPARTMENT (Dep_ID, Name, Year_Established, Department_Head_Name, Address, Telephone, No_of_Employee) VALUES (101, 'Computer Science', 2005, 'Dr. Smith', '123 Main St, Mumbai', '555-1234', 50), (102, 'Electrical Engineering', 1998, 'Dr. Singh', '456 Elm St, Mumbai', '555-5678', 70), (103, 'Mechanical Engineering', 2012, 'Dr. Lee', '789 Maple Ave, Pune', '555-9012', 60), (104, 'Civil Engineering', 2007, 'Dr. Patel', '321 Oak Rd, Pune', '555-3456', 55), (105, 'Chemical Engineering', 2003, 'Dr. Gupta', '987 High St, Mumbai', '555-7890', 65), (106, 'Bioengineering', 2015, 'Dr. Chen', '654 Pine St, Mumbai', '555-2345', 45), (107, 'Aerospace Engineering', 2010, 'Dr. Kumar', '321 Oak Rd, Pune', '555-6789', 75), (108, 'Materials Science and Engineering', 2009, 'Dr. Jones', '123 Main St, Pune', '555-0123', 80), (109, 'Environmental Engineering', 2013, 'Dr. Kim', '456 Elm St, Pune', '555-4567', 40), (110, 'Industrial and Systems Engineering', 2006, 'Dr. Nguyen', '789 Maple Ave, Mumbai', '555-8901', 90); (i) List the name and address of departments which are at least 5 years old. SELECT Name, Address FROM DEPARTMENT WHERE YEAR(CURDATE()) - Year_Established >= 5; +---------------------------------+-----------------------+ | Name | Address | +---------------------------------+-----------------------+ | Computer Science | 123 Main St, Mumbai | | Electrical Engineering | 456 Elm St, Mumbai | | Civil Engineering | 321 Oak Rd, Pune | | Chemical Engineering | 987 High St, Mumbai | | Materials Science and Engineering | 123 Main St, Pune | | Industrial and Systems Engineering | 789 Maple Ave, Mumbai | +---------------------------------+-----------------------+ (ii) Find the name of head of department having department id = 202. SELECT Department_Head_Name FROM DEPARTMENT WHERE Dep_ID = 202; +-----------------------+ | Department_Head_Name | +-----------------------+ | Dr. Gupta | +-----------------------+ (iii) List name, address and telephone numbers of all the departments which are established after year 2010 SELECT Name, Address, Telephone FROM DEPARTMENT WHERE Year_Established > 2010; +---------------------------------+-----------------------+------------+ | Name | Address | Telephone | +---------------------------------+-----------------------+------------+ | Mechanical Engineering | 789 Maple Ave, Pune | 555-9012 | | Environmental Engineering | 456 Elm St, Pune | 555-4567 | | Aerospace Engineering | 321 Oak Rd, Pune | 555-6789 | | Bioengineering | 654 Pine St, Mumbai | 555-2345 | +---------------------------------+-----------------------+------------+ 3) Create the following table and perform the necessary tasks defined below : (a) Create the following table named Book : (i) Tittle (ii) ISBN_Number (iii) Author (iv) Year_of_publication (v) Publisher (vi) Price CREATE TABLE Book ( Title VARCHAR(255), ISBN_Number VARCHAR(50), Author VARCHAR(255), Year_of_publication INT, Publisher VARCHAR(255), Price DECIMAL(10, 2) ); (b) Enter at least 10 records in the above table and answer the following queries using SQL : INSERT INTO Book (Title, ISBN_Number, Author, Year_of_publication, Publisher, Price) VALUES ('The Great Gatsby', '9780743273565', 'F. Scott Fitzgerald', 2004, 'Scribner', 9.49); INSERT INTO Book (Title, ISBN_Number, Author, Year_of_publication, Publisher, Price) VALUES ('To Kill a Mockingbird', '9780446310789', 'Harper Lee', 1988, 'Grand Central Publishing', 7.19); INSERT INTO Book (Title, ISBN_Number, Author, Year_of_publication, Publisher, Price) VALUES ('The Catcher in the Rye', '9780316769174', 'J. D. Salinger', 1991, 'Back Bay Books', 8.99); INSERT INTO Book (Title, ISBN_Number, Author, Year_of_publication, Publisher, Price) VALUES ('1984', '9780451524935', 'George Orwell', 2003, 'Signet Classics', 9.99); INSERT INTO Book (Title, ISBN_Number, Author, Year_of_publication, Publisher, Price) VALUES ('Animal Farm', '9780451526342', 'George Orwell', 2003, 'Signet Classics', 7.99); INSERT INTO Book (Title, ISBN_Number, Author, Year_of_publication, Publisher, Price) VALUES ('The Hobbit', '9780547928227', 'J.R.R. Tolkien', 2012, 'Mariner Books', 12.39); INSERT INTO Book (Title, ISBN_Number, Author, Year_of_publication, Publisher, Price) VALUES ('The Lord of the Rings', '9780544003415', 'J.R.R. Tolkien', 2012, 'Mariner Books', 18.99); INSERT INTO Book (Title, ISBN_Number, Author, Year_of_publication, Publisher, Price) VALUES ('The Da Vinci Code', '9780307474278', 'Dan Brown', 2009, 'Anchor', 8.99); INSERT INTO Book (Title, ISBN_Number, Author, Year_of_publication, Publisher, Price) VALUES ('Harry Potter and the Philosopher\'s Stone', '9781408855652', 'J.K. Rowling', 2014, 'Bloomsbury Publishing', 10.99); INSERT INTO Book (Title, ISBN_Number, Author, Year_of_publication, Publisher, Price) VALUES ('The Hunger Games', '9780439023528', 'Suzanne Collins', 2010, 'Scholastic Press', 7.19); (i) List the title and ISBN - Number of all the books which are published in year 2012 by "Popular Publisher". SELECT Title, ISBN_Number FROM Book WHERE Year_of_publication = 2012 AND Publisher = 'Popular Publisher'; (ii) List the title and price of all the books published by "ABC publisher". SELECT Title, Price FROM Book WHERE Publisher = 'ABC publisher'; (iii) Find the authors name and price of book with ISBN_Number = "110056389". SELECT Author, Price FROM Book WHERE ISBN_Number = '110056389'; 4) Write a query in SQL to create a table employee and department. Employee(empno,ename,deptno,job,hiredate) Department(deptno,dname,loc) A. Include the following constraints on column of Employee table. a) make empno as primary key of the table b) ename attribute does not contain NULL values c) job attribute allow only UPPERCASE entries d) put the current date as default date in hire date column in case data is not supplied for the column. B. Include the following constraints on column of dept table. a) make deptno as primary key. b) dname,loc attributes does not contain NULL values c)enforce REFERENTIAL INTEGRITY where deptno attribute of dept table as primary key and deptno attribute of emp table as foreign key. -- create employee table CREATE TABLE employee ( empno INT PRIMARY KEY, ename VARCHAR(50) NOT NULL, deptno INT, job VARCHAR(50) CHECK(job = UPPER(job)), hiredate DATE DEFAULT CURRENT_DATE ); -- create department table CREATE TABLE department ( deptno INT PRIMARY KEY, dname VARCHAR(50) NOT NULL, loc VARCHAR(50) NOT NULL ); -- add foreign key constraint to employee table ALTER TABLE employee ADD CONSTRAINT fk_deptno FOREIGN KEY (deptno) REFERENCES department(deptno); 5) Write a query in SQL to create a table employee and department with following attributes. Employee(empno,ename,deptno,job,hiredate) Department(deptno,dname,loc) 1. Give list of emp name & their job spec who are working in dept no 20? 2. Retrieve the details of emp working in dept no 30 ? 3. Find list of emp whose empno is greater then manager no ? 4. Find all manager not working in dept no 10 ? 5. To find the total number of employees. CREATE TABLE Employee ( empno INT PRIMARY KEY, ename VARCHAR(50) NOT NULL, deptno INT, job VARCHAR(50) CHECK (job = UPPER(job)), hiredate DATE DEFAULT CURRENT_DATE, mgr INT ); CREATE TABLE Department ( deptno INT PRIMARY KEY, dname VARCHAR(50) NOT NULL, loc VARCHAR(50) NOT NULL ); -- Populate Employee and Department tables with some data for demonstration purposes INSERT INTO Employee (empno, ename, deptno, job, hiredate, mgr) VALUES (7369, 'SMITH', 20, 'CLERK', '1980-12-17', 7902), (7499, 'ALLEN', 30, 'SALESMAN', '1981-02-20', 7698), (7521, 'WARD', 30, 'SALESMAN', '1981-02-22', 7698), (7566, 'JONES', 20, 'MANAGER', '1981-04-02', 7839), (7654, 'MARTIN', 30, 'SALESMAN', '1981-09-28', 7698), (7698, 'BLAKE', 30, 'MANAGER', '1981-05-01', 7839), (7782, 'CLARK', 10, 'MANAGER', '1981-06-09', 7839), (7788, 'SCOTT', 20, 'ANALYST', '1982-12-09', 7566), (7839, 'KING', 10, 'PRESIDENT', '1981-11-17', NULL), (7844, 'TURNER', 30, 'SALESMAN', '1981-09-08', 7698), (7876, 'ADAMS', 20, 'CLERK', '1983-01-12', 7788), (7900, 'JAMES', 30, 'CLERK', '1981-12-03', 7698), (7902, 'FORD', 20, 'ANALYST', '1981-12-03', 7566), (7934, 'MILLER', 10, 'CLERK', '1982-01-23', 7782); INSERT INTO Department (deptno, dname, loc) VALUES (10, 'ACCOUNTING', 'NEW YORK'), (20, 'RESEARCH', 'DALLAS'), (30, 'SALES', 'CHICAGO'), (40, 'OPERATIONS', 'BOSTON'); -- Query 1: Give list of emp name & their job spec who are working in dept no 20 SELECT ename, job FROM Employee WHERE deptno = 20; -- Query 2: Retrieve the details of emp working in dept no 30 SELECT * FROM Employee WHERE deptno = 30; -- Query 3: Find list of emp whose empno is greater then manager no SELECT * FROM Employee WHERE empno > mgr; -- Query 4: Find all manager not working in dept no 10 SELECT * FROM Employee WHERE job = 'MANAGER' AND deptno <> 10; -- Query 5: To find the total number of employees SELECT COUNT(*) FROM Employee; 6) Write a query in sql to create a table employee and department. Employee(empno,ename,deptno,job,hiredate) Department(deptno,dname,loc) 1. To find the total number of clerk hired after 13-Jan-1981. 2. Determine which department having more than two people holding a same job? 3. Find all department that have at least two clerk? 4. Retrieve emp name and job who have the same job as that of ‘Allen’? 5. List all emp name and their job of those department that are located at Chicago? -- Create Employee table CREATE TABLE Employee ( empno INT PRIMARY KEY, ename VARCHAR(50) NOT NULL, deptno INT, job VARCHAR(50), hiredate DATE DEFAULT GETDATE() ); -- Create Department table CREATE TABLE Department ( deptno INT PRIMARY KEY, dname VARCHAR(50) NOT NULL, loc VARCHAR(50) ); -- To find the total number of clerk hired after 13-Jan-1981. SELECT COUNT(*) AS total_clerks_hired FROM Employee WHERE job = 'CLERK' AND hiredate > '1981-01-13'; -- Determine which department having more than two people holding a same job? SELECT deptno, job, COUNT(*) AS num_employees FROM Employee GROUP BY deptno, job HAVING COUNT(*) > 2; -- Find all department that have at least two clerk? SELECT deptno FROM Employee WHERE job = 'CLERK' GROUP BY deptno HAVING COUNT(*) >= 2; -- Retrieve emp name and job who have the same job as that of ‘Allen’? SELECT ename, job FROM Employee WHERE job = (SELECT job FROM Employee WHERE ename = 'Allen'); -- List all emp name and their job of those department that are located at Chicago? SELECT e.ename, e.job FROM Employee e JOIN Department d ON e.deptno = d.deptno WHERE d.loc = 'Chicago'; 7) Write a query in sql to create a table employee and department. Employee(empno,ename,deptno,job,hiredate,salary) Department(deptno,dname,loc) CREATE TABLE Employee ( empno INT PRIMARY KEY, ename VARCHAR(50) NOT NULL, deptno INT, job VARCHAR(50), hiredate DATE, salary FLOAT ); CREATE TABLE Department ( deptno INT PRIMARY KEY, dname VARCHAR(50) NOT NULL, loc VARCHAR(50) ); 1. Find list of emp whose salary is greater then 20,000 along with their job profile ? SELECT ename, job FROM Employee WHERE salary > 20000; 2. Give list of all emp along with salary working for deptno=5? SELECT ename, salary FROM Employee WHERE deptno = 5; 3. List all empname who are ordered by the jobs profile in ascending order as well as empno in descending order SELECT ename, job FROM Employee ORDER BY job ASC, empno DESC; 4. Retrieve details of emp whose salary is between 1000 and 2000 both inclusive? SELECT * FROM Employee WHERE salary BETWEEN 1000 AND 2000; 5. To find the average salary of all the employees. SELECT AVG(salary) as avg_salary FROM Employee; 8) Write a query in sql to create a table employee and department. Employee(empno,ename,deptno,job,hiredate,salary) Department(deptno,dname,loc) CREATE TABLE Employee ( empno INT PRIMARY KEY, ename VARCHAR(50) NOT NULL, deptno INT, job VARCHAR(50) CHECK(job = UPPER(job)), hiredate DATE DEFAULT CURRENT_DATE, salary INT ); CREATE TABLE Department ( deptno INT PRIMARY KEY, dname VARCHAR(50) NOT NULL, loc VARCHAR(50) ); 1. To find names of all employee working for Mumbai Location SELECT ename FROM Employee e, Department d WHERE e.deptno = d.deptno AND d.loc = 'Mumbai'; +--------+ | ename | +--------+ | John | | Mike | | Sarah | +--------+ 2. List annual salary from all job containing more than one employee? SELECT job, SUM(salary*12) AS annual_salary FROM employee GROUP BY job HAVING COUNT(*) > 1; | job | annual_salary | |------------|--------------| | ANALYST | 192000 | | CLERK | 120000 | | MANAGER | 540000 | | SALESMAN | 216000 | 3. List all dept having atleast one employee with analyst job profile? SELECT DISTINCT d.dname FROM department d INNER JOIN employee e ON d.deptno = e.deptno WHERE e.job = 'ANALYST'; +-------+ | dname | +-------+ | IT | | HR | +-------+ 4. To find the minimum salary of managers in various depts.? SELECT deptno, MIN(salary) AS min_salary FROM Employee WHERE job LIKE '%manager%' GROUP BY deptno; deptno | min_salary -------|----------- 10 | 40000 20 | 30000 30 | 35000 5. To select all employees sorted dept wise in ascending order and within dept salary wise in descending order for working for dept no. 10 and 20. SELECT empno, ename, deptno, job, salary FROM Employee WHERE deptno IN (10, 20) ORDER BY deptno ASC, salary DESC; empno | ename | deptno | job | salary -------+----------+--------+----------------+-------- 7788 | SCOTT | 10 | ANALYST | 3000.00 7839 | KING | 10 | PRESIDENT | 5000.00 7934 | MILLER | 10 | CLERK | 1300.00 7566 | JONES | 20 | MANAGER | 2975.00 7902 | FORD | 20 | ANALYST | 3000.00 7876 | ADAMS | 20 | CLERK | 1100.00 7698 | BLAKE | 30 | MANAGER | 2850.00 7654 | MARTIN | 30 | SALESMAN | 1250.00 7521 | WARD | 30 | SALESMAN | 1250.00 7499 | ALLEN | 30 | SALESMAN | 1600.00 7900 | JAMES | 30 | CLERK | 950.00 7844 | TURNER | 30 | SALESMAN | 1500.00 (12 rows) 9) Write a query in sql to create a table employee and department. Employee(empno,ename,deptno,job,hiredate) Department(deptno,dname,loc) 1. List all jobs which are common in dept 10 and 20 ? 2. List all jobs which are present in dept 10 but not in dep 20? 3. List all managers with their respective name and deptno ? 4. Display list of emp whose job is analyst ? 5. To list all the employees hired during the year 1982 -- Create tables CREATE TABLE Employee ( empno INT PRIMARY KEY, ename VARCHAR(50) NOT NULL, deptno INT, job VARCHAR(50) CHECK(job = UPPER(job)), hiredate DATE DEFAULT CURRENT_DATE, salary INT ); CREATE TABLE Department ( deptno INT PRIMARY KEY, dname VARCHAR(50) NOT NULL, loc VARCHAR(50) ); -- Insert sample data INSERT INTO Employee VALUES (1, 'John', 10, 'ANALYST', '1982-01-01', 5000), (2, 'Alice', 20, 'MANAGER', '1982-02-01', 8000), (3, 'Bob', 10, 'CLERK', '1982-03-01', 2500), (4, 'Carol', 20, 'ANALYST', '1982-04-01', 6000), (5, 'Dave', 10, 'MANAGER', '1982-05-01', 7000); INSERT INTO Department VALUES (10, 'Research', 'New York'), (20, 'Sales', 'Chicago'), (30, 'Operations', 'Dallas'); -- List all jobs which are common in dept 10 and 20 SELECT DISTINCT job FROM Employee WHERE deptno = 10 AND job IN (SELECT job FROM Employee WHERE deptno = 20); -- List all jobs which are present in dept 10 but not in dep 20 SELECT DISTINCT job FROM Employee WHERE deptno = 10 AND job NOT IN (SELECT job FROM Employee WHERE deptno = 20); -- List all managers with their respective name and deptno SELECT ename, deptno FROM Employee WHERE job = 'MANAGER'; -- Display list of emp whose job is analyst SELECT * FROM Employee WHERE job = 'ANALYST'; -- To list all the employees hired during the year 1982 SELECT * FROM Employee WHERE hiredate BETWEEN '1982-01-01' AND '1982-12-31'; 10) Write a query in sql to create a table employee and department. Employee(empno,ename,deptno,job,hiredate,salary) Department(deptno,dname,loc) 1. To get all employees working for dept 10 and 20. 2. To list all employees whose name begins with ‘J’. 3. Retrieve all details of employees whose name is either Smith, Blake, Allen, Scott, Clark and King ? 4. Create view on appropriate tables to display ename , job , salary , dept name? 5. Drop the above view -- Create tables CREATE TABLE Employee ( empno INT PRIMARY KEY, ename VARCHAR(50) NOT NULL, deptno INT, job VARCHAR(50), hiredate DATE, salary FLOAT ); CREATE TABLE Department ( deptno INT PRIMARY KEY, dname VARCHAR(50) NOT NULL, loc VARCHAR(50) ); -- Insert some data INSERT INTO Employee VALUES (7369, 'SMITH', 20, 'CLERK', '1980-12-17', 800.00); INSERT INTO Employee VALUES (7499, 'ALLEN', 30, 'SALESMAN', '1981-02-20', 1600.00); INSERT INTO Employee VALUES (7521, 'WARD', 30, 'SALESMAN', '1981-02-22', 1250.00); INSERT INTO Employee VALUES (7566, 'JONES', 20, 'MANAGER', '1981-04-02', 2975.00); INSERT INTO Employee VALUES (7654, 'MARTIN', 30, 'SALESMAN', '1981-09-28', 1250.00); INSERT INTO Employee VALUES (7698, 'BLAKE', 30, 'MANAGER', '1981-05-01', 2850.00); INSERT INTO Employee VALUES (7782, 'CLARK', 10, 'MANAGER', '1981-06-09', 2450.00); INSERT INTO Employee VALUES (7788, 'SCOTT', 20, 'ANALYST', '1982-12-09', 3000.00); INSERT INTO Employee VALUES (7839, 'KING', 10, 'PRESIDENT', '1981-11-17', 5000.00); INSERT INTO Employee VALUES (7844, 'TURNER', 30, 'SALESMAN', '1981-09-08', 1500.00); INSERT INTO Employee VALUES (7876, 'ADAMS', 20, 'CLERK', '1983-01-12', 1100.00); INSERT INTO Employee VALUES (7900, 'JAMES', 30, 'CLERK', '1981-12-03', 950.00); INSERT INTO Employee VALUES (7902, 'FORD', 20, 'ANALYST', '1981-12-03', 3000.00); INSERT INTO Employee VALUES (7934, 'MILLER', 10, 'CLERK', '1982-01-23', 1300.00); INSERT INTO Department VALUES (10, 'ACCOUNTING', 'NEW YORK'); INSERT INTO Department VALUES (20, 'RESEARCH', 'DALLAS'); INSERT INTO Department VALUES (30, 'SALES', 'CHICAGO'); INSERT INTO Department VALUES (40, 'OPERATIONS', 'BOSTON'); -- Get all employees working for dept 10 and 20 SELECT * FROM Employee WHERE deptno IN (10, 20); -- List all employees whose name begins with ‘J’ SELECT * FROM Employee WHERE ename LIKE 'J%'; -- Retrieve all details of employees whose name is either Smith, Blake, Allen, Scott, Clark and King SELECT * FROM Employee WHERE ename IN ('SMITH', 'BLAKE', 'ALLEN', 'SCOTT', 'CLARK', 'KING'); -- Create a view to display ename, job, salary, dept name CREATE VIEW EmployeeDetails AS SELECT e.ename, e.job, e CREATE VIEW emp_dept_view AS SELECT e.ename, e.job, e.salary, d.dname FROM Employee e INNER JOIN Department d ON e.deptno = d.deptno; Drop view 11) Write a query in sql to create a table employee and department. Employee(empno,ename,deptno,job,hiredate,salary) Department(deptno,dname,loc) 1. To list employees working in dept other than 10. 2. To find the minimum salaries of various categories of employees in various depts. 3. To find the minimum salaries of various categories of employees, department wise such that minimum salary is greater than 1500. 4. Create view on emp displaying ename , job ,sal for employee working in dept 10. 5. Drop the above view. -- Creating tables CREATE TABLE Employee ( empno INT PRIMARY KEY, ename VARCHAR(50) NOT NULL, deptno INT, job VARCHAR(50), hiredate DATE, salary INT ); CREATE TABLE Department ( deptno INT PRIMARY KEY, dname VARCHAR(50), loc VARCHAR(50) ); -- Inserting data INSERT INTO Employee (empno, ename, deptno, job, hiredate, salary) VALUES (7369, 'Smith', 20, 'Clerk', '1980-12-17', 800), (7499, 'Allen', 30, 'Salesman', '1981-02-20', 1600), (7521, 'Ward', 30, 'Salesman', '1981-02-22', 1250), (7566, 'Jones', 20, 'Manager', '1981-04-02', 2975), (7654, 'Martin', 30, 'Salesman', '1981-09-28', 1250), (7698, 'Blake', 30, 'Manager', '1981-05-01', 2850), (7782, 'Clark', 10, 'Manager', '1981-06-09', 2450), (7788, 'Scott', 20, 'Analyst', '1982-12-09', 3000), (7839, 'King', 10, 'President', '1981-11-17', 5000), (7844, 'Turner', 30, 'Salesman', '1981-09-08', 1500), (7876, 'Adams', 20, 'Clerk', '1983-01-12', 1100), (7900, 'James', 30, 'Clerk', '1981-12-03', 950), (7902, 'Ford', 20, 'Analyst', '1981-12-03', 3000), (7934, 'Miller', 10, 'Clerk', '1982-01-23', 1300); INSERT INTO Department (deptno, dname, loc) VALUES (10, 'Accounting', 'New York'), (20, 'Research', 'Dallas'), (30, 'Sales', 'Chicago'); -- To list employees working in dept other than 10 SELECT * FROM Employee WHERE deptno <> 10; -- To find the minimum salaries of various categories of employees in various depts SELECT job, deptno, MIN(salary) FROM Employee GROUP BY job, deptno; -- To find the minimum salaries of various categories of employees, department wise such that minimum salary is greater than 1500 SELECT job, deptno, MIN(salary) FROM Employee WHERE salary > 1500 GROUP BY job, deptno; -- Creating view CREATE VIEW emp_dept10 AS SELECT ename, job, salary FROM Employee WHERE deptno = 10; -- Dropping view DROP VIEW emp_dept10; 12) Write a query in sql to create a table employee and department. Employee(empno,ename,deptno,job,hiredate,salary) Department(deptno,dname,loc) Q-1> To select the employees whose salary is greater than the salary of all employees working in dept no. 30 Q-2> To list all employees in the ascending order by name. Q-3> To select all employees sorted dept wise in ascending order and within dept salary wise in descending order. Q-4> To select all employees working in location whose name is starting with L Q-5> To find the minimum salary of managers in various depts. CREATE TABLE Employee( empno INT PRIMARY KEY, ename VARCHAR(50), deptno INT, job VARCHAR(50), hiredate DATE, salary FLOAT ); CREATE TABLE Department( deptno INT PRIMARY KEY, dname VARCHAR(50), loc VARCHAR(50) ); INSERT INTO Employee VALUES (7369, 'SMITH', 20, 'CLERK', '1980-12-17', 800), (7499, 'ALLEN', 30, 'SALESMAN', '1981-02-20', 1600), (7521, 'WARD', 30, 'SALESMAN', '1981-02-22', 1250), (7566, 'JONES', 20, 'MANAGER', '1981-04-02', 2975), (7654, 'MARTIN', 30, 'SALESMAN', '1981-09-28', 1250), (7698, 'BLAKE', 30, 'MANAGER', '1981-05-01', 2850), (7782, 'CLARK', 10, 'MANAGER', '1981-06-09', 2450), (7788, 'SCOTT', 20, 'ANALYST', '1982-12-09', 3000), (7839, 'KING', 10, 'PRESIDENT', '1981-11-17', 5000), (7844, 'TURNER', 30, 'SALESMAN', '1981-09-08', 1500), (7876, 'ADAMS', 20, 'CLERK', '1983-01-12', 1100), (7900, 'JAMES', 30, 'CLERK', '1981-12-03', 950), (7902, 'FORD', 20, 'ANALYST', '1981-12-03', 3000), (7934, 'MILLER', 10, 'CLERK', '1982-01-23', 1300); INSERT INTO Department VALUES (10, 'ACCOUNTING', 'NEW YORK'), (20, 'RESEARCH', 'DALLAS'), (30, 'SALES', 'CHICAGO'); -- Q-1> To select the employees whose salary is greater than the salary of all employees working in dept no. 30 SELECT * FROM Employee WHERE salary > ALL (SELECT salary FROM Employee WHERE deptno = 30); -- Q-2> To list all employees in the ascending order by name. SELECT * FROM Employee ORDER BY ename ASC; -- Q-3> To select all employees sorted dept wise in ascending order and within dept salary wise in descending order. SELECT * FROM Employee ORDER BY deptno ASC, salary DESC; -- Q-4> To select all employees working in location whose name is starting with L SELECT * FROM Employee e JOIN Department d ON e.deptno = d.deptno WHERE d.loc LIKE 'L%'; -- Q-5> To find the minimum salary of managers in various depts. SELECT deptno, MIN(salary) AS min_salary FROM Employee WHERE job = 'MANAGER' GROUP BY deptno; -- Create view on emp displaying ename, job, and salary for employees working in dept 10. CREATE VIEW emp_dept_10 AS SELECT ename, job, salary FROM Employee WHERE deptno = 10; -- Drop the above view. DROP VIEW emp_dept_10; 13) Creat the following table and insert the given value. Employees: Emp_id Name 01 Hansen,Ola 02 Svendson, Tova 03 Svendson, Stephen 04 Pettersen, Kari Orders: Prod_id Product Emp_id 234 Printer 01 657 Table 03 865 Chair 03 Q-1> List details of employee who ordered a product along with what did they order. Q-2> List details of employee who ordered a printer. Q-3> List all the employees who dint not purchase any product Q-4> Retrieve product details whose emp_id = 01 CREATE TABLE Employees ( Emp_id INT PRIMARY KEY, Name VARCHAR(255) ); INSERT INTO Employees (Emp_id, Name) VALUES (01, 'Hansen,Ola'), (02, 'Svendson, Tova'), (03, 'Svendson, Stephen'), (04, 'Pettersen, Kari'); CREATE TABLE Orders ( Prod_id INT PRIMARY KEY, Product VARCHAR(255), Emp_id INT, FOREIGN KEY (Emp_id) REFERENCES Employees(Emp_id) ); INSERT INTO Orders (Prod_id, Product, Emp_id) VALUES (234, 'Printer', 01), (657, 'Table', 03), (865, 'Chair', 03); -- Q-1 SELECT e.Emp_id, e.Name, o.Product FROM Employees e INNER JOIN Orders o ON e.Emp_id = o.Emp_id; -- Q-2 SELECT e.Emp_id, e.Name, o.Product FROM Employees e INNER JOIN Orders o ON e.Emp_id = o.Emp_id WHERE o.Product = 'Printer'; -- Q-3 SELECT e.Emp_id, e.Name FROM Employees e LEFT JOIN Orders o ON e.Emp_id = o.Emp_id WHERE o.Prod_id IS NULL; -- Q-4 SELECT o.Prod_id, o.Product, o.Emp_id FROM Orders o WHERE o.Emp_id = 01; 14) CREATE TABLE Person ( driver_id INT PRIMARY KEY, name VARCHAR(50), address VARCHAR(100) ); CREATE TABLE Car ( license VARCHAR(10) PRIMARY KEY, model VARCHAR(50), year INT ); CREATE TABLE Accident ( report_number INT PRIMARY KEY, date DATE, location VARCHAR(100) ); CREATE TABLE Owns ( driver_id INT, license VARCHAR(10), PRIMARY KEY (driver_id, license), FOREIGN KEY (driver_id) REFERENCES Person(driver_id), FOREIGN KEY (license) REFERENCES Car(license) ); CREATE TABLE Participated ( driver_id INT, license VARCHAR(10), report_number INT, damage VARCHAR(50), amount INT, PRIMARY KEY (driver_id, license, report_number), FOREIGN KEY (driver_id, license) REFERENCES Owns(driver_id, license), FOREIGN KEY (report_number) REFERENCES Accident(report_number) ); INSERT INTO Person VALUES (1, 'John Smith', '123 Main St'), (2, 'Sachin Parker', '456 Oak Ave'), (3, 'Maria Rodriguez', '789 Maple Blvd'); INSERT INTO Car VALUES ('ABC123', 'Honda Civic', 2010), ('DEF456', 'Ford Mustang', 2015), ('GHI789', 'SKODA Rapid', 2018); INSERT INTO Accident VALUES (1, '1999-07-01', 'Main St and Elm St'), (2, '1999-08-15', 'Oak Ave and Maple Blvd'), (3, '2000-01-01', 'Elm St and Maple Blvd'); INSERT INTO Owns VALUES (1, 'ABC123'), (2, 'DEF456'), (3, 'GHI789'), (1, 'DEF456'); INSERT INTO Participated VALUES (1, 'ABC123', 1, 'Scratch', 500), (2, 'DEF456', 1, 'Dent', 1000), (1, 'DEF456', 2, 'Scratch', 750), (3, 'GHI789', 3, 'Total loss', 5000); -- (i) Create relations persons owns in SQL CREATE VIEW persons_owns AS SELECT p.name, p.driver_id, c.license FROM Person p INNER JOIN Owns o ON p.driver_id = o.driver_id INNER JOIN Car c ON o.license = c.license; -- (ii) Add a new accident to the database INSERT INTO Accident VALUES (4, '2000-02-15', 'Maple Blvd and Main St'); -- (iii) Delete the SKODA belonging to 'Sachin Parker' DELETE FROM Owns WHERE license = 'GHI789' AND driver_id = (SELECT driver_id FROM Person WHERE name = 'Sachin Parker'); -- (iv) Find the total number of people who owned cars that were involved in accident in 1999 SELECT COUNT(DISTINCT o.driver_id) AS total_owners FROM Owns o INNER JOIN Participated p ON o.driver_id = p.driver_id AND o.license = p.license INNER JOIN Accident a ON p.report_number = a.report_number WHERE YEAR(a.date) = 1999; -- (v) Find the person whose names starts with 'S' and arrange in decreasing order of driver-id SELECT name, driver_id FROM Person WHERE name LIKE 'S%' ORDER BY driver_id