Advertisement
xbeji

Lab 2

Oct 22nd, 2024
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 5.07 KB | None | 0 0
  1.  
  2.  
  3.  
  4. --***Calculated Fields****:
  5. -- to see what will be the salary of each employee if they all get
  6. -- an increase of 10%, type and run
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28. SELECT Empno, Sal*0.1
  29. FROM Emp;
  30.  
  31. ---list the employee number, name and salary of each employee if they
  32. -- all get a 100% salary increase.
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46. SELECT Empno, Ename, Sal, Sal+Sal  
  47. FROM Emp;
  48.  
  49. ---Write a query to list employee number, employee name, job and yearly
  50. -- (12 months) salary for each employee after the 100% increase.
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. SELECT empno, ename, sal, (Sal+sal)*12  
  63. FROM Emp;
  64.  
  65.  
  66. ---***The WHERE clause***
  67.  
  68. -- list the employee numbers, names and department number of only those
  69. -- employees belonging to department 10, type and run
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82. SELECT Empno, Ename, Deptno
  83. FROM Emp
  84. WHERE Deptno = 10;
  85.  
  86. --list the names and salaries of all Salesmen, type and run
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101. SELECT Ename, Sal, Job
  102. FROM Emp
  103. WHERE Job = 'Salesman';
  104.  
  105. --- If the salary of each Salesman is reduced by 200, produce a list of
  106. --  all Salesmen showing their new salaries.
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120. SELECT empno, ename, sal, sal-200 AS Netsalary
  121. FROM Emp;
  122.  
  123.  
  124.  
  125.  
  126. -------------------- EXERCISE LAB2: *** FUNCTIONS ***-----------------
  127.  
  128. --1)FIND OUT HOW MANY MANAGERS THERE ARE WITHOUT LISTING THEM.
  129. -- coun (job)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.         SELECT COUNT (JOB ) AS "NUMBER OF MANAGERS"
  144.         FROM EMP
  145.         WHERE JOB = 'Manager';
  146.  
  147.  
  148.  
  149. --2)COMPUTE THE AVERAGE ANNUAL SALARY + COMMISSION FOR ALL SALESMEN  
  150.     -- AVG(SAL+COMM)*12
  151.  
  152.    
  153.    
  154.    
  155.    
  156.    
  157.    
  158.    
  159.    
  160.    
  161.    
  162.  
  163.         SELECT AVG ( SAL+COMM ) * 12 AS "ANNUAL NET SALARY"
  164.         FROM EMP
  165.         WHERE JOB = 'Salesman';
  166.  
  167. --3)FIND THE HIGHEST AND LOWEST SALARIES
  168. --  MAX(SAL), MIN(SAL)
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.         SELECT MAX ( SAL ), MIN ( SAL )
  180.         FROM EMP ;
  181.  
  182.  
  183. -- 4) FIND THE NUMBER OF CHARACTERS IN THE LONGEST DEPARTMENT NAME
  184.    --  MAX(LENGTH(DNAME))
  185.    
  186.    
  187.    
  188.    
  189.    
  190.    
  191.    
  192.    
  193.    
  194.    
  195.    
  196.         SELECT MAX (LENGTH (DNAME ) )
  197.         FROM DEPT ;
  198.  
  199.  -- 5) COUNT THE NUMBER OF PEOPLE IN DEPARTMENT 30 WHO RECEIVE A SALARY
  200.  --    AND THE NUMBER OF PEOPLE WHO RECEIVE A COMMISSION
  201. --     COUNT(SAL), COUNT(COMM)
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.         SELECT COUNT ( SAL ), COUNT (COMM)
  216.         FROM EMP
  217.         WHERE DEPTNO = 30;
  218.  
  219.  
  220. -- 6) COMPUTE THE AVERAGE COMMISSION OF EMPLOYEES WHO RECEIVE A COMMISSION
  221. --    AND THE AVERAGE COMMISSION OF ALL EMPLOYEES (TREATING EMPLOYEES WHO
  222. --    DO NOT RECEIVE A COMMISSION AS RECEIVING A ZERO COMMISSION)
  223. --    AVG(COMM), AVG(NVL(COMM,0))
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.         SELECT AVG ( COMM ), AVG (NVL (COMM, 0 ) )
  243.         FROM EMP;
  244.                             -- NVL() converts a null value to an actual value.
  245.        
  246.        
  247. --7) LIST THE AVERAGE SALARY OF EMPLOYEES THAT RECEIVE A SALARY, THE
  248.  --  AVERAGE COMMISSION OF EMPLOYEES THAT RECEIVE A COMMISSION, THE
  249. --   AVERAGE SALARY PLUS COMMISSION OF ONLY THOSE EMPLOYEES THAT RECEIVE
  250. --   A COMMISSION AND AVERAGE SALARY PLUS COMMISSION OF ALL EMPLOYEES
  251.  --  INCLUDING THOSE WHO DO NOT RECEIVE A COMMISSION
  252.  
  253.  --  AVG(SAL), AVG(COMM), AVG(SAL+COMM), AVG(SAL+NVL(COMM,0))
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.     SELECT AVG (SAL), AVG ( COMM ), AVG ( SAL + COMM ),
  267.     AVG (SAL + NVL(COMM, 0 ) )
  268.     FROM EMP;
  269.  
  270.    
  271.  
  272. --8) COMPUTE THE DAILY AND HOURLY SALARIES FOR EMPLOYEES IN DEPARTMENT
  273.   -- 30. ROUND THE RESULTS TO THE NEAREST PENNY.  ASSUME THERE ARE 22
  274.   -- WORKING DAYS IN A MONTH AND 8 WORKING HOURS IN A DAY.
  275.  
  276.  
  277.  
  278.    
  279.    
  280.    
  281.    
  282.    
  283.  
  284.  
  285.         SELECT ENAME, SAL AS MONTHLY, ROUND ( SAL/22, 2 )AS DAILY,
  286.         ROUND ( SAL/ (22* 8 ) , 2 )AS HOURLY
  287.         FROM EMP
  288.         WHERE DEPTNO = 30;
  289.        
  290.        
  291.        
  292. -- 9) ISSUE THE SAME QUERY AS THE PREVIOUS ONE EXCEPT THAT THIS TIME
  293.    -- TRUNCATE (TRUNC) TO THE NEAREST PENNY RATHER THAN ROUND.
  294.    
  295.    
  296.    
  297.    
  298.    
  299.    
  300.    
  301.    
  302.    
  303.         SELECT ENAME, SAL AS MONTHLY, TRUNC ( SAL/22, 2 ) AS DAILY,
  304.         TRUNC ( SAL/ (22 * 8 ) , 1 ) AS HOURLY
  305.         FROM EMP
  306.         WHERE DEPTNO = 30;
  307.  
  308.  
  309. --- Create New table and name it student
  310.  
  311. CREATE TABLE student (
  312. Sid CHAR (10) PRIMARY KEY ,
  313. Sname varchar2 (20),
  314. Sphone NUMBER(10));
  315.  
  316. DESC student;
  317.  
  318.  
  319. -- Insert the following Values:
  320.  
  321.  
  322. INSERT INTO student VALUES (10, 'Ahmad', 05400000);
  323. INSERT INTO student VALUES (20, 'Omar', 05400000);
  324. INSERT INTO student VALUES (30, 'hassan', 05400000);
  325.  
  326. SELECT * FROM student;
  327.  
  328. -- Delete row or taple from a table:
  329.  
  330. DELETE FROM student
  331. WHERE Sid='30';
  332.  
  333. -- Delete all data from a table:
  334.  
  335. DELETE FROM student;
  336.  
  337. -- ALTER ---> ADD, DROP, MODIFY (DDL)
  338.  
  339.       --- Add New column for table:
  340.      
  341.         ALTER TABLE student ADD Address varchar2 (20);
  342.  
  343.     --- Delete column from a table:
  344.    
  345.         ALTER TABLE student DROP COLUMN Address;
  346.        
  347.     -- MODIFY a column inside a table:
  348.    
  349.         ALTER TABLE student MODIFY Sphone varchar2 (20);
  350.        
  351.     -- Delete row from a table:
  352.        
  353.         DELETE FROM student WHERE sid = 10;
  354.        
  355.     -- Update single value  in table
  356.        
  357.         UPDATE student
  358.         SET Sname= 'Khalid'
  359.         WHERE Sid=20;
  360.        
  361.     -- Update Multivalues in table
  362.        
  363.         UPDATE student
  364.         SET Sname= 'ali'
  365.        
  366.     -- Delete table from Database :
  367.    
  368.         DROP TABLE student;
  369.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement