Advertisement
debabrataMukherjee

Untitled

Jun 14th, 2023
1,437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 10.36 KB | Source Code | 0 0
  1. SHOW DATABASES;
  2.  
  3. -- -----------------
  4. -- Creating Table
  5. -- -----------------
  6. CREATE TABLE Employee (
  7.     E_No CHAR(3) PRIMARY KEY,
  8.     E_Name VARCHAR(50) NOT NULL,
  9.     Job_Type VARCHAR(50) NOT NULL,
  10.     Manager CHAR(3),
  11.     Hire_Date DATE NOT NULL,
  12.     D_No CHAR(3) NOT NULL,
  13.     Commission DECIMAL(10 , 2 ),
  14.     Salary DECIMAL(10 , 2 ) NOT NULL,
  15.     Total_Salary Decimal(10,2) as (Commission+Salary)
  16. );
  17. CREATE TABLE Department (
  18.     D_No CHAR(3) PRIMARY KEY,
  19.     D_Name VARCHAR(50),
  20.     Location VARCHAR(50) NOT NULL
  21. );
  22. -- --------------------------
  23. -- For deleting the tables
  24. -- --------------------------
  25. drop table Employee;
  26. drop table department;
  27.  
  28.  
  29. -- ------------------------------------
  30. -- For Inserting values in the table
  31. -- ------------------------------------
  32. insert into Employee
  33. (E_No, E_Name, Job_Type, Manager, Hire_Date, D_No, Commission, Salary)
  34. values
  35. ('E01','Debabrata','Engineer','YES','2021-4-4','D01',4578.5,78000),
  36. ('E02','Debapriya','Engineer','NO','2021-4-4','D01',69878.5,68000),
  37. ('E03','Ananta','Programmer','NO','2021-4-8','D02',978.5,58000),
  38. ('E04','Rick','Editor','NO','2021-4-14','D03',51078.5,50000),
  39. ('E05','Debajeet','Editor','NO','2021-8-16','D03',1878.5,48000),
  40. ('E06','Prithiviraj','Receptionist','NO','2021-8-16','D04',0.0,28000),
  41. ('E07','Anish','GateMan','NO','2022-4-24','D05',20000.0,18000);
  42.  
  43.  
  44. insert into Department
  45. values
  46. ('D01','Department1','Mumbai'),
  47. ('D02','Department2','Delhi'),
  48. ('D03','Department3','Delhi'),
  49. ('D04','Department4','Delhi'),
  50. ('D05','Department5','Delhi');
  51.  
  52.  
  53. -- ------------------------------------
  54. -- Displaying the values in the table
  55. -- ------------------------------------
  56. SELECT
  57.     *
  58. FROM
  59.     Employee;
  60. SELECT
  61.     *
  62. FROM
  63.     department;
  64.  
  65.  
  66. -- -------------------------------
  67. -- Different Types OF Query
  68. -- -------------------------------
  69.  
  70. SELECT
  71.     Employee.E_No,
  72.     Employee.E_Name,
  73.     Employee.Job_Type,
  74.     Employee.Hire_Date,
  75.     Employee.Total_Salary,
  76.     Department.D_Name,
  77.     Department.Location
  78. FROM
  79.     Employee
  80.         JOIN
  81.     Department ON Employee.D_No = Department.D_No
  82. ORDER BY Employee.E_Name ASC;
  83.  
  84.  -- 1. Query to display Employee Name, Job, Hire Date, Employee Number; for each employee with the
  85.     SELECT
  86.     E_No, E_Name, Job_Type, Hire_Date
  87. FROM
  88.     Employee;
  89.  
  90.  -- 2. Query to display unique Jobs from the Employee Table.
  91.  
  92. SELECT
  93.     E_No, E_Name, Job_Type, Hire_Date
  94. FROM
  95.     Employee
  96. WHERE
  97.     Job_Type = 'Editor';
  98.    
  99.  -- Example 2
  100. SELECT
  101.     E_No, E_Name, Job_Type, Hire_Date
  102. FROM
  103.     Employee
  104. WHERE
  105.     Job_Type = 'Engineer';
  106.    
  107. -- 3. Query to display the Employee Name concatenated by a Job separated by a comma.
  108.  
  109. SELECT
  110.     CONCAT(E_Name, ' , ', Job_Type) AS Concatenated_Name
  111. FROM
  112.     Employee;
  113.  
  114. -- 4. Query to display all the data from the Employee Table. Separate each Column by a comma and
  115. --    name the said column as THE_OUTPUT.
  116. SELECT
  117.     CONCAT(E_No,
  118.             ' , ',
  119.             E_Name,
  120.             ' , ',
  121.             Job_Type,
  122.             ' , ',
  123.             Manager,
  124.             ' , ',
  125.             Hire_Date,
  126.             ' , ',
  127.             D_No,
  128.             ' , ',
  129.             Commission,
  130.             ' , ',
  131.             Salary,
  132.             ' , ',
  133.             Total_Salary) AS THE_OUTPUT
  134. FROM
  135.     Employee;
  136.  
  137. -- 5. Query to display the Employee Name and Salary of all the employees
  138. SELECT
  139.     E_Name, Total_Salary
  140. FROM
  141.     Employee
  142. WHERE
  143.     Total_Salary >= 50000;
  144.  
  145. -- 6. Query to display Employee Name and Department Number for the Employee No= E04.
  146. SELECT
  147.     E_Name, D_No
  148. FROM
  149.     Employee
  150. WHERE
  151.     E_No = 'E04';
  152.  
  153. -- 7. Query to display Employee Name and Salary for all employees whose salary
  154. SELECT
  155.     E_Name, Total_Salary
  156. FROM
  157.     Employee
  158. WHERE
  159.     NOT (Total_Salary > 40000
  160.         AND Total_Salary < 50000);
  161.        
  162. -- 8. Query to display Employee Name and Department No. of all the employees
  163. SELECT
  164.     E_Name, D_No
  165. FROM
  166.     Employee
  167. WHERE
  168.     D_No = 'D01' OR D_No = 'D03'
  169. ORDER BY E_Name ASC;
  170.  
  171.  
  172. -- 9. Query to display Name and Hire Date of every Employee who
  173. --    was hired in 1981.
  174.  
  175. SELECT
  176.     E_Name, Hire_Date
  177. FROM
  178.     employee
  179. WHERE
  180.     YEAR(Hire_Date) = '2021';
  181.  
  182. -- 10. Query to display Name and Job of all employees who don’t
  183. --     have a current Manager.
  184. SELECT
  185.     E_Name, Job_Type
  186. FROM
  187.     employee
  188. WHERE
  189.     Manager = 'NO'
  190.  
  191. -- 11. Query to display the Name, Salary and Commission for all the
  192. --     employees who earn commission.
  193. SELECT
  194.     E_Name, Salary, Commission
  195. FROM
  196.     employee
  197. WHERE
  198.     Commission > 0.0
  199.  
  200. -- 12. Sort the data in descending order of Salary and Commission.
  201.  
  202. SELECT
  203.     Salary, Commission
  204. FROM
  205.     employee
  206. ORDER BY salary DESC , commission DESC
  207.  
  208. -- *** 13. Query to display Name of all the employees where the third
  209. --     letter of their name is ‘A’ or 'B'.
  210.  
  211. SELECT
  212.     Total_Salary, E_Name
  213. FROM
  214.     Employee
  215. WHERE
  216.     SUBSTRING(E_Name, 3, 1) = 'A'
  217.         OR SUBSTRING(E_Name, 3, 1) = 'B'*** 14. Query to display Name of all employees either have two ‘R’s
  218. --     or have two ‘ A’s in their name and are either in Dept No = 30
  219. --     or their Manger’s Employee No = 7788.
  220. SELECT
  221.     E_Name
  222. FROM
  223.     Employee
  224. WHERE
  225.     ((LENGTH(E_Name) - LENGTH(REPLACE(LOWER(E_Name), 'r', ''))) >= 2
  226.         OR (LENGTH(E_Name) - LENGTH(REPLACE(LOWER(E_Name), 'a', ''))) >= 2)
  227.         AND (D_No = 'D01' OR Manager = 'YES');
  228.  
  229.        
  230. -- 15. Query to display Name, Salary and Commission for all employees
  231. --     whose Commission Amount is greater than their Salary increased by 5%.
  232.  
  233. SELECT
  234.     E_Name, Salary, Commission
  235. FROM
  236.     Employee
  237. WHERE
  238.     (Commission > (Salary * 21 / 20));
  239.  
  240.  -- 16. Query to display the Current Date.
  241.  
  242. SELECT CURDATE() AS Todays_Date;
  243.  
  244.  -- 17. Query to display Name, Hire Date and Salary Review Date which is the
  245.  --     1st Monday after six months of employment.
  246.  
  247. SELECT
  248.     E_Name,
  249.     Hire_Date,
  250.     DATE_ADD(DATE_ADD(DATE_ADD(Hire_Date, INTERVAL 6 MONTH),
  251.             INTERVAL (9 - WEEKDAY(DATE_ADD(Hire_Date, INTERVAL 6 MONTH))) DAY),
  252.         INTERVAL IF(WEEKDAY(DATE_ADD(Hire_Date, INTERVAL 6 MONTH)) >= 1,
  253.             7,
  254.             0) - WEEKDAY(DATE_ADD(Hire_Date, INTERVAL 6 MONTH)) DAY) AS salary_review_date
  255. FROM
  256.     Employee;
  257.  
  258.  
  259.  
  260. -- 18. Query to display Name and calculate the number of months between today
  261. --     and the date each employee was hired.
  262.  
  263. SELECT
  264.     E_Name,
  265.     TIMESTAMPDIFF(MONTH,
  266.         Hire_Date,
  267.         CURDATE()) AS months_since_hire
  268. FROM
  269.     Employee;
  270.  
  271.  
  272. -- 19. Query to display the following for each employee <E-Name> earns < Salary>
  273. --     monthly but wants < 3 * Current Salary >. Label the Column as Dream Salary.
  274.  
  275. SELECT
  276.     CONCAT(E_Name,
  277.             ' earns ',
  278.             FORMAT(Salary, 2),
  279.             ' monthly but wants ',
  280.             FORMAT(3 * Salary, 2)) AS 'Dream Salary'
  281. FROM
  282.     Employee;
  283.  
  284.  
  285.  
  286. -- 20. Query to display Name with the 1st letter capitalized and all other letter
  287. --     lower case and length of their name of all the employees whose name starts
  288. --     with ‘J’, ’A’ and ‘M’.
  289.  
  290.  
  291.  
  292.  
  293. -- 21. Query to display Name, Hire Date and Day of the week on which the employee started.
  294.  
  295.  
  296.  
  297.  
  298. -- 22. Query to display Name, Department Name and Department No for all the employees.
  299.  
  300.  
  301.  
  302. -- 23. Query to display Unique Listing of all Jobs that are in Department # 30.
  303.  
  304.  
  305.  
  306. -- 24. Query to display Name, Dept Name of all employees who have an ‘A’ in their name.
  307.  
  308.  
  309.  
  310.  
  311. -- 25. Query to display Name, Job, Department No. And Department Name for all
  312. --     the employees working at the Dallas location.
  313.  
  314.  
  315.  
  316.  
  317. -- 26. Query to display Name and Employee no. Along with their Manger’s Name and
  318. --     the Manager’s employee no; along with the Employees’ Name who do not have a Manager.
  319.  
  320.  
  321.  
  322.  
  323. -- 27. Query to display Name, Dept No. And Salary of any employee whose department
  324. --     No. and salary matches both the department no. And the salary of any employee
  325. --     who earns a commission.
  326.  
  327.  
  328.  
  329.  
  330. -- 28. Query to display Name and Salaries represented by asterisks, where each asterisk
  331. --     (*) signifies $100.
  332.  
  333.  
  334.  
  335.  
  336.  
  337. -- 29. Query to display the Highest, Lowest, Sum and Average Salaries of all the employees
  338. SELECT
  339.     MAX(Total_Salary) AS highest_salary,
  340.     MIN(Total_Salary) AS lowest_salary,
  341.     SUM(Total_Salary) AS total_salary,
  342.     AVG(Total_Salary) AS average_salary
  343. FROM
  344.     Employee;
  345.  
  346.  
  347.  
  348. -- *** 30. Query to display the number of employees performing the same Job type functions.
  349.  
  350. SELECT
  351.     Job_Type, COUNT(*) AS NoOF_Employees
  352. FROM
  353.     Employee
  354. GROUP BY Job_Type;
  355.  
  356.  
  357.  
  358. -- 31. Query to display the no. of managers without listing their names.
  359.  
  360.  
  361.  
  362.  
  363. -- 32. ***Query to display the Department Name, Location Name, No. of Employees and the average
  364. --     salary for all employees in that department.
  365.  
  366. SELECT
  367.     Department.D_name,
  368.     Department.Location,
  369.     COUNT(Employee.E_No) AS No_Of_employees,
  370.     AVG(Employee.Total_Salary) AS Avg_Salary
  371. FROM
  372.     Department
  373.         JOIN
  374.     Employee ON Department.D_No = Employee.D_No
  375. GROUP BY Department.D_Name , Department.Location;
  376.  
  377.  
  378. -- 33. Query to display Name and Hire Date for all employees in the same dept. as Blake.
  379.  
  380. SELECT
  381.     E_Name, Hire_Date
  382. FROM
  383.     Employee
  384. WHERE
  385.     D_No = (SELECT
  386.             D_No
  387.         FROM
  388.             Employee
  389.         WHERE
  390.             E_Name = 'Rick');
  391.  
  392.  
  393.  
  394. -- *** 34. Query to display the Employee No. And Name for all employees who earn more than
  395. --     the average salary.
  396. SELECT
  397.     E_No, E_Name
  398. FROM
  399.     Employee
  400. WHERE
  401.     Total_Salary > (SELECT
  402.             AVG(Total_Salary)
  403.         FROM
  404.             Employee);
  405.  
  406.  
  407. -- 35. Query to display Employee Number and Name for all employees who work in a department
  408. --     with any employee whose name contains a ‘T’.
  409.  
  410.  
  411.  
  412.  
  413.  
  414. -- 36. Query to display the names and salaries of all employees who report to King.
  415.  
  416.  
  417.  
  418.  
  419.  
  420. -- 37. Query to display the department no, name and job for all employees in the Sales department.
  421.  
  422. SELECT
  423.     Employee.D_No, Employee.E_Name, Employee.Job_Type
  424. FROM
  425.     Employee
  426.         JOIN
  427.     Department ON Employee.D_No = Department.D_No
  428. WHERE
  429.     Department.D_Name = 'Department2';
  430.  
  431.    
  432. -- **Use to delete tables from database**
  433.  
  434. drop table employee;
  435. drop table department;
  436.  
  437.    
  438.  
  439.  
  440.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement