New Text DocumentS
New Text DocumentS
Write a query to create a table employee with empno, ename, designation and
salary.
-- 6. Write a query to display the column name and data type of the table employee
desc employee;
-- 7. Write a query to create a table from an existing table with all the fields.
-- 8. Write a query to create table from an existing table with selected fields.
-- 9. Write a query to create a new table from an existing table without any
record.
-- 10. Write a query to Alter the column empno number(4) to empno number(6).
-- 11. Write a query to Alter the table employee with multiple columns (empno,
ename).
-- 5. Create a table department with attribute d_name, d_loc and HOD_id where
d_name is primary key.
-- 9. Make the department attribute of the employee table its foreign key
referencing the department table.
-- 19. Show the employees f_name , l_name , salary and the salary after 1000rs
Bonus.
-- 20. Show the employees annual salary with a 1000rs. Yearly bonus and the annual
salary with a 100rs Monthly bonus.
-- 21. Show f_name as NAME and annual salary as ANNSAL from the employee table.
-- 22. Show the l_name as LasT AND 100rs. Incremented salary as NewSal.
-- 23. Show the emp_id, f_name, l_name, job_type of the employee getting highest
salary.
-- 24. Show the emp_id, f_name, l_name, job_type of the employee getting minimum
salary.
-- 26. Consider the Insurance database given below. The primary keys are underlined
and the data types are specified:
-- i> Create the above tables by properly specifying the primary keys and the
foreign keys
-- iii> Demonstrate how you a. Update the damage amount for the car with a specific
regno in accident with report number 12 to 25000 b. Add a new accident to the
database
UPDATE participated
SET damage_amount = 25000
WHERE regno = 'DEF456' AND report_number = 12;
-- iv> Find the total number of people who owned cars that were involved in
accidents in 2006.
-- v> Find the number of accidents in which cars belonging to a specific model were
involved.
-- 6. Show the employee names who gets salary of more than 50000 per month.
-- 8. Show the employee details whose salary ranges between 40000 and 70000.
-- 9. Show the details of the employees who works under the manager having id 1, 6
and 8.
-- 10. Select the f_name and salary of those employees whose last name starts with
‘K’.
-- 11. Select the f_name and salary of those employees whose last name starts with
‘K’ and ends with ‘R’.
-- 12. Show the details of those employees where 3rd letter of l_name is ‘o’.
-- 13. Select the details of those employees who works as an engineer with monthly
salary more than 50000.
SELECT *FROM employee
WHERE job_type = 'Engineer' AND salary > 50000;
-- 15. Find the minimum salary, maximum salary, total salary, average salary of the
employees who work in ‘Sales’ department.
-- 16. Find the employee l_name that is first and f_name that is last if they are
arranged in an order
SELECT l_name, f_name FROM employee ORDER BY l_name ASC, f_name DESC;
-- 22. Find the department name and average salaries of those departments whose
average salary is greater than 40000
-- 23. Find the department name and maximum salaries of those departments whose
maximum salary is greater than 55000
-- 24. Display the job_type and total monthly salary for each job_type where total
payroll is exceeding 100000
-- 25. Display the name of the department having maximum average salary.
-- a. Sqrt()
-- b. Power()
-- c. Ceil()
-- d. Substr()
-- e. Max()
-- f. min()
-- g. Round()
-- h. avg()
-- i. count()
-- k. mod()
-- c. Calculate 8^7.
-- 6. Find the total experience of the employees in weeks who works in Sales
department.
-- From this point Onwards nothing will work in MYSQL and can only be done in
ORACLE --
-- a. Months_between
-- b. Add_months
-- c. Next_day
SELECT NEXT_DAY(TO_DATE('2023-01-25', 'YYYY-MM-DD'), 'TUESDAY') AS next_day FROM
DUAL;
-- d. Last_day
-- e. Round
-- f. Trunc
-- g. To_char
-- 8. Show the employee details with a revised salary. The salary is incremented in
the following way:
-- 9. Determine the tax for each employee in production department based on the
monthly salary. The tax rate are as per the following data:
-- 10. Find the Cartesian product between Employee and Department table.
-- 11. Show the employee names and the respective department location.
-- 12. Give an example of the following joins considering employee and department
tables.
-- l. Inner join
-- FOR MYSQL -
-- 3. Find the employee name and date of joining who are working in Delhi.
-- 5. Display the name of employees, department location and the city name the
employee belongs to, from the Employee, Department and Emp_Address tables.
-- 8. Display the employee names with their respective job grades and salary.
-- 9. Insert two rows in Employee table having ‘NULL’ values in dept field.
-- 11. Perform the following joins considering Employee and Department tables.
-- a. Natural join
-- b. Inner join
-- FOR MYSQL -
-- 1. Write An SQL Query To Fetch “FIRST_NAME” From Worker Table In Upper Case
alias as WORKER_FIRSTNAME.
-- 2. Write An SQL Query To Print The First Three Characters Of FIRST_NAME From
Worker Table.
-- 3. Write An SQL Query To Find The Position Of The Alphabet (‘A’) In The First
Name Column ‘Amitabh’ From Worker Table.
-- 4. Write An SQL Query To Print The FIRST_NAME And LAST_NAME From Worker Table
Into A Single Column COMPLETE_NAME. A Space Char Should Separate Them.
-- 5. Write An SQL Query To Print All Worker Details From The Worker Table Order By
FIRST_NAME Ascending And DEPARTMENT Descending.
-- 6. Write An SQL Query To Print Details Of The Workers Whose FIRST_NAME Contains
‘A’.
-- 7. Write An SQL Query To Print Details Of The Workers Whose FIRST_NAME Ends With
‘A’.
-- 8. Write An SQL Query To Print Details Of The Workers Whose SALARY Lies Between
100000 And 500000.
-- 9. Write An SQL Query To Fetch The Count Of Employees Working In The Department
‘Admin’.
-- 10. Write An SQL Query To Fetch The No. Of Workers For Each Department In The
Descending Order.
SELECT COUNT(*) AS NO_OF_EMPLOYEE, DEPARTMENT FROM Worker
GROUP BY DEPARTMENT
ORDER BY NO_OF_EMPLOYEE DESC;
-- 11. Write An SQL Query To Print Details Of The Workers Who Are Also Managers.
-- 12. Write An SQL Query To Show Only Odd Rows From A Table.
-- 13. Write An SQL Query To Show Records From One Table That Another Table Does
Not Have.
-- 14. Write An SQL Query To Show The Top N (Say 10) Records Of A Table.
-- 15. Write An SQL Query To Fetch The List Of Employees With The Same Salary.
-- 16. Write An SQL Query To Show All Departments Along With The Number Of People
Working There.
-- 17. Write An SQL Query To Print The Name Of Employees Having The Highest Salary
In Each Department.
-- 18. Write An SQL Query To Fetch Departments Along With The Total Salaries Paid
For Each Of Them.
-- i) Create the above tables by properly specifying the primary keys and the
foreign keys.
CREATE TABLE CUSTOMER(
Cust int(25) NOT NULL PRIMARY KEY,
Cname char(25),
City char(25)
);
-- iv) List the Order# for the orders that were shipped from all the warehouses
that the company has in a specific city
-- v) Demonstrate how you delete Item# 10 from the ITEM table and make that field
null in the ORDER- ITEM table.
UPDATE Emp
SET e_phone = '86487988'
WHERE e_no = '5';
-- i) Create the reference between Emp and Dept table with e_no attribute.
UPDATE Dept
SET dept_hod = 'Rajju'
WHERE dept_no = '20';
-- i) Write a query to find the employee name and dept_hod whose dept_hod is SAY,
“John”.
-- ii) Write a query to find the average salary of the employee of CSE department.
-- 3. Display the previous and current department and job_types of all the
employees.
-- 4. Display the employee id and job_types of the employees who currently have a
job title that they held previously.
-- 5. Find the name of those employees who have not changed their jobs once.
-- 6. Find the names of the employees who earn more than Chitra.
-- 7. Find the details of those employees who have the same job_type as of emp_id
7.
-- 8. Find the details of the employees whose job_type is same as that of emp_id 3
and whose salary is greater than that of emp_id 7.
SELECT *FROM employee
WHERE job_type = (SELECT job_type FROM employee WHERE Emp_id = 3) AND salary >
(SELECT salary FROM employee WHERE Emp_id = 7);
-- 9. Display l_name, job_type and the salary of the employees whose salary is
equal to the minimum salary.
-- 11. Display all the departments that have a minimum salary greater than that of
‘Sales’ department.
-- 12. Find the employees who earn the same salary for each department.
SELECT d_name
FROM Department
WHERE (SELECT MIN(salary) FROM employee WHERE job_type = 'Sales') < ALL
(SELECT MIN(salary) FROM employee WHERE job_type = Department.d_name)
GROUP BY d_name;
-- 13. Display the employees who are not engineers and whose salary is less than
that of any engineer.
-- 14. Display the employees whose salary is less than the salary of all employees
with a job_type ‘Clerk’ and whose job_type is not ‘Clerk’.
-- 15. Consider the following database of students enrollment in courses and books
adopted for each course.
-- i) Create the above tables by properly specifying the primary keys and the
foreign keys
-- iii) Demonstrate how you add a new text book to the database and make this book
be adopted by some department.
-- Step 1: Insert the new textbook information into the "TEXT" table
INSERT INTO TEXT (book_isbn, book_title, publisher, author)
VALUES (2754, 'Exploring Mysteries', 'Knowledge World', 'Sophia');
-- v) List any department that has all its adopted books published by a specific
publisher.
-- i) Create above tables by properly specifying the primary keys and the foreign
keys.
-- iii) Give the details of the authors who have 2 or more books in the catalog and
the price of the books is greater than the average price of the books in the
catalog and the year of publication is after 2010.
-- iv) Find the author of the book which has maximum sales.
--For Oracle
WITH AuthorSales AS (
SELECT C.author_id, SUM(O.quantity) AS total_sales
FROM catalogue C
JOIN orderdetails O ON C.book_id = O.book_id
GROUP BY C.author_id
ORDER BY SUM(O.quantity) DESC
)
SELECT A.author_id, A.author_name
FROM author A
JOIN AuthorSales ASales ON A.author_id = ASales.author_id
WHERE ROWNUM = 1;
--For Mysql
WITH AuthorSales AS (
SELECT C.author_id, SUM(O.quantity) AS total_sales
FROM catalogue C
JOIN orderdetails O ON C.book_id = O.book_id
GROUP BY C.author_id
ORDER BY SUM(O.quantity) DESC
)
SELECT A.author_id, A.author_name
FROM author A
JOIN AuthorSales ASales ON A.author_id = ASales.author_id
LIMIT 1;
UPDATE catalogue c
SET c.price = c.price + (c.price * 0.1)
WHERE c.publisher_id = 2002 ;
-- i) Create the above tables by properly specifying the primary keys and foreign
keys.
-- iii) Find all the customers who have atleast two accounts at the main branch.
-- iv) Find all customer who have an account at all the branches located in a
specific city.
-- i) Create the above tables by properly specifying the primary keys and the
foreign keys
-- iii) List the order number and ship date for all orders shipped from particular
warehouse
-- v) List the orders that were not shipped within 30 days of ordering
DECLARE
a NUMBER := 10;
b NUMBER := 20;
c NUMBER := 15;
largest NUMBER;
BEGIN
IF (a >= b) AND (a >= c) THEN
largest := a;
ELSIF (b >= a) AND (b >= c) THEN
largest := b;
ELSE
largest := c;
END IF;
DBMS_OUTPUT.PUT_LINE('The largest number among ' || a || ', ' || b || ', and '
|| c || ' is ' || largest);
END;
DECLARE
n NUMBER(4) := 5;
s NUMBER(4) := 8;
r NUMBER(4);
BEGIN
WHILE n > 0 LOOP
r := MOD(n, 10);
s := (s * 10) + r;
n := TRUNC(n / 10);
END LOOP;
DECLARE
n NUMBER(4) := 5;
f NUMBER(4) := 1;
result NUMBER(4);
BEGIN
FOR i IN 1..n LOOP
f := f * i;
END LOOP;
result := f;
DECLARE
n NUMBER(4) := 3;
is_prime BOOLEAN := TRUE;
BEGIN
IF n <= 1 THEN
is_prime := FALSE;
ELSE
FOR i IN 2..TRUNC(SQRT(n)) LOOP
IF MOD(n, i) = 0 THEN
is_prime := FALSE;
EXIT;
END IF;
END LOOP;
END IF;
IF is_prime THEN
DBMS_OUTPUT.put_line(n || ' is a prime number.');
ELSE
DBMS_OUTPUT.put_line(n || ' is not a prime number.');
END IF;
END;
DECLARE
n NUMBER(4) := 10;
a NUMBER(4) := 0;
b NUMBER(4) := 1;
fib NUMBER(4);
BEGIN
DBMS_OUTPUT.put_line('Fibonacci series up to ' || n || ':');
IF n >= 1 THEN
DBMS_OUTPUT.put_line(a);
END IF;
IF n >= 2 THEN
DBMS_OUTPUT.put_line(b);
END IF;
-- 6. Write a PL/SQL program for calculating sum of two numbers. [NOT WORKING]
DECLARE
num1 NUMBER := 10;
num2 NUMBER := 20;
sum_result NUMBER;
BEGIN
sum_result := num1 + num2;
DBMS_OUTPUT.PUT_LINE('The sum of ' || num1 || ' and ' || num2 || ' is: ' ||
sum_result);
END;
-- 7. Write a PL/SQL program to check the given year is leap year or not
DECLARE
year_to_check NUMBER := 2016;
is_leap BOOLEAN := FALSE;
BEGIN
-- Check if the year is divisible by 4
IF (MOD(year_to_check, 4) = 0) THEN
IF (MOD(year_to_check, 100) <> 0 OR MOD(year_to_check, 400) = 0) THEN
is_leap := TRUE;
END IF;
END IF;
IF is_leap THEN
DBMS_OUTPUT.PUT_LINE(year_to_check || ' is a leap year.');
ELSE
DBMS_OUTPUT.PUT_LINE(year_to_check || ' is not a leap year.');
END IF;
END;
DECLARE
number_to_sum NUMBER := 566;
digit_sum NUMBER := 0;
temp_number NUMBER;
BEGIN
temp_number := number_to_sum;
DECLARE
input_number NUMBER := 123456789; --
num_even NUMBER := 0;
num_odd NUMBER := 0;
temp_number NUMBER;
digit NUMBER;
BEGIN
temp_number := input_number;