SQL Assignment

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 9

SQL Assignment

1. The following SELECT statement executes successfully:

SQL> SELECT last_name, job_id, salary AS Sal FROM employees; (TRUE/FALSE)

true

2. The following SELECT statement executes successfully:

SQL> SELECT * FROM job_grades; (TRUE/FALSE)

false

3. There are four coding errors in the following statement. Can you identify them?

SQL> SELECT employee_id, last_name sal x 12 ANNUAL SALARY FROM employees;

SELECT employee_id, last_name, salary x 12 “ANNUAL SALARY” FROM employees

One error is there is no comma(,) between last_name and sal coloumn;

Second error is sal coloumn name not available in table employees;

There is no operator x;

If there is space,then coloumn alias should be enclosed between “”

4. If you concatenate a NULL value with a character string, the result is NULL. (TRUE/FALSE)

false

5. Write a query to display all columns and all rows from employees table.

select * from employees;

6. Which arithmetic operations are valid for DATE and TIMESTAMP data?

Date+number,date-number,date-date,date+number/24

Addition and subtraction;

7. Write a query to display employee number, employee name and to calculate a salary increase of

2500 for all employees.

Select employee_id,first_name,last_name,salary+2500 as “SALARY” from employees;

Select employee_id,first_name||last_name “employee_name”,salary+2500 as “SALARY” from

employees;
8. What is the default rule of precedence in arithmetic expression?

*,/,+,-

9. Write down the syntax to display the structure of the DEPARTMENTS table.

Desc departments;

10. Write a query to display the last name, job code, hire date, and employee number for each

employee, with the employee number appearing first. Provide an alias STARTDATE for the

HIRE_DATE column.

Select employee_id,last_name,job_id,hire_date as “HIRE_DATE” from employees;

Select employee_id,last_name,job_id,hire_date as “STARTDATE” from employees;

11. Write a query to display all unique job codes from the EMPLOYEES table.

Select distinct(JOB_ID) from employees;

12. The HR department has requested a report of all employees and their job IDs. Display the last

name concatenated with the job ID (separated by a comma and space) and name the column

Employee and Title.

Select last_name ||’, ‘||job_id as “Employee and Title” From employees;

13. Write a query to display all the data from employees table. Separate each column output with a

comma. Name the column THE_OUTPUT.

SELECT EMPLOYEE_ID||’,’||FIRST_NAME||’,’||LAST_NAME||’,’||EMAIL||’,’||PHONE_NUMBER||’,’||

HIRE_DATE||’,’||JOB_ID||’,’||SALARY||’,’||COMMISSION_PCT||’,’||MANAGER_ID||’,’||

DEPARTMENT_ID

AS “THE_OUTPUT” FROM EMPLOYEES;

14. Write a query to display the last name and salary of employees who earn more than $12,000.

Select last_name,salary from employees where salary>12000;

15. Write a query to display the last name and department number for employee number 176.

Select last_name,department_id form employees where employee_id=176;


16. Write a query to display the last name, job ID, and hire date for employees whose last names

are Matos or Taylor. Order the query in ascending order by hire date.

Select last_name,job_id,hire_date from employees where last_name=’Matos’ or

last_name=’Taylor’ order by hire_date;

Select last_name,job_id,hire_date from employees where LAST_NAME in(‘Matos’,’Taylor’) order

by hire_date;

17. Write a query to display the last name and department number of all employees in departments

20 or 50 in ascending alphabetical order by last name.

Select last_name,department_id from employees where department_id=20 or department_id=50

Order by last_name;

Select last_name,department_id from employees where department_id in(20,50) Order by

last_name;

18. Values that are specified with the BETWEEN condition are exclusive. (TRUE/FALSE).

True;

false

19. Write a query to display the last name and salary of employees who earn between $5,000 and

$12,000, and are in department 20 or 50. Label the columns Employee and Monthly Salary,

respectively.

Select last_name as “Employee”,salary as “Monthly Salary” from employees where salary

between 5000 and 12000 or (department_id =20 or department_id=50);

20. Column alias can be used in WHERE condition (TRUE/FALSE).

False;

21. Write a query to display the last name and hire date for all employees who were hired in 1994.

Select last_name,hire_date from employees where hire_date like(‘%94’);

22. Write a query to display the last name and first name of all employees who do not have a

manager.

Select last_name,first_name from employees where manager_id is null;

LAST_NAME FIRST_NAME

------------------------- --------------------

King Steven

23. In Oracle, character values are not case sensitive. (TRUE/FALSE).

False.
24. While using BETWEEN operators, either lower limit or upper limit can be used first. (TRUE/FALSE)

false

25. Write a query to display employee numbers, last names, and salaries for all the employees

whose manager’s employee number is not 100, 200 and 300.

Select employee_id,last_name,salary from employees where employee_id not in(100,200,300);

Select employee_id,last_name,salary from employees where manager_id not in(100,200,300);

26. Column alias can be used in ORDER BY clause (TRUE/FALSE).

False

true

27. Write a query to display the last name, salary, and commission of all employees whose

commission has been decided. Sort the data in descending order of salary and commissions.

Select last_name,salary commission_pct from employees where commission_pct is not null order

by salary desc,commission_pct ;

28. Which symbols are used for wildcard searching with LIKE operator? Describe about those.

% and _ are used as wildcard characters. Both are used in like operator where exact value is not

known to search where _ represents any single character and % represents any sequence of

zeros and characters;

% and _ are used as wildcard characters. Both are used in like operator where exact value is not

known to search where _ represents any single character and % represents any number of

characters;

29. Write a query to display employee number, last name and salary of all employees, who are

working in department number 40 or their name first name contains an ‘e’ as third letter.

Select employee_id,last_name,ssalary from employees where department_id =40 or first_name

like(‘__e%’);

30. What is the use of BETWEEN operator, IN operator and LIKE operator in Oracle?

Between operator- It’s used to specify range;

IN operator- It’s used to specify list of values;

LIKE operator- It’s used exact values are not known to search;

LIKE operator- It’s used to search when exact values are not known

31. Write a query to display the lastname and salary of all employee in which the third letter of the

firstname is “a.”

select last_name,salary from employees where first_name like(‘__a%’);


32. Write a query to display employee number and last name of all employees whose job id contains

SA_.

Select employee_id,last_name from employees where job_id like(‘SA\_%’)

Escape(‘\’);

33. Write a query to display the last names of all employees who have both an a and an e in their

last name.

Select last_name from employees where last_name like(‘%a%e%’);

Select last_name from employees where last_name like(‘%a%e%’) or last_name like(‘%e%a

%');

34. Write a query to display the last name, job, and salary for all employees whose jobs are either

that of a sales representative or a stock clerk, and whose salaries are not equal to $2,500,

$3,500, or $7,000.

Select last_name,job_id,salary from employees where (job_id=’ ST_CLERK’’ And salary not in

(2500,3500,7000))or (job_id=’ SA_REP’ And salary not in (2500,3500,7000));

35. Write a query to display the current date. Label the column as Date.

Select sysdate as “Date” from dual;

36. Write a query to display the employee number, last name, salary, and salary increased by 15.5%

(expressed as a whole number) for each employee. Label the column “New Salary”.

Select employee_id,last_name,salary,salary*(1+15.5/100) as “New Salary”;

Select employee_id,last_name,salary,round(salary*(1+15.5/100)) as “New Salary” from

employees;

37. Single-row functions can not modify the data type (TRUE/FALSE).

false

38. Write down three examples of GENERAL FUNCTIONS available in Oracle SQL.

NVL,NVL2,NULL IF

nullif

39. LPAD function is an example of case-manipulation function (TRUE/FALSE).

false
40. Write a query that displays the last name (with the first letter uppercase and all other letters

lowercase) and the length of the last name for all employees whose name starts with the letters

J, A, or M. Sort the results by the last names of the employees.

Select initcap(last_name),length(last_name) from employees where first_name like(‘J%’) or

first_name like(‘A%’) or first_name like(‘M%’) order by last_name;

41. Write a query to display the last name and calculate the number of months between today and

the date on which the employee was hired. Label the column MONTHS_WORKED. Order your

results by the number of months employed. Round the number of months up to the closest whole

number.

Select last_name,round(months_between(sysdate,hire_date),0) as “MONTHS_WORKED” from

employees order by months_between(sysdate,hire_date);

42. Write a query to produce the following for each employee:

<employee last name> earns <salary> monthly but wants <3 times salary>. Label the

column Dream Salaries.

Select last_name,salary,3*salary as “Dream Salaries” from employees;

Select last_name||’earns’||salary||’ monthly but wants’||3*salary as “Dream Salaries”

from employees;

43. Write a query to display the last name and salary for all employees. Format the salary to be 15

characters long, left-padded with the $ symbol. Label the column SALARY.

Select last_name,lpad(salary,15,’$’) as “SALARY” from employees;

44. Which SQL function is used to return the last day of a month?

Last_day

45. Write a query to display each employee’s last name, hire date, and salary review date, which is

the first Monday after six months of service. Label the column REVIEW.

Select last_name,hire_date,next_day(add_months(hire_date,6),’MONDAY’) as “salary review

date” from employees;

46. SYSDATE is a SQL function which returns current date, time and time zone information

(TRUE/FALSE).

false

47. Write a query to display the last name, hire date, and day of the week on which an employee

started. Label the column DAY. Order the results by the day of the week.

Select last_name,hire_date,to_char(hire_date,’day’) as “DAY” from employees order by DAY;


48. Write a query to display the employees’ last names and commission amounts. If an employee

does not earn a commission, show “No Commission.” Label the column COMM.

Select last_name,nvl(to_char(commission_pct),’ No Commission’) as “comm.” From employees;

49. The ROUND and TRUNCATE function can be used for NUMBER and DATE values. (TRUE/FALSE)

true

50. Current date can be subtracted from older date (TRUE/FALSE).

true

51. Using the CASE function, write a query that displays the grade of all employees based on the

value of the JOB_ID column, using the following data:

Job Grade

AD_PRES A

ST_MAN B

IT_PROG C

SA_REP D

ST_CLERK E

None of the above 0

52. Group functions work across many rows to produce one result per group (TRUE/FALSE).

true

53. The WHERE clause restricts rows before inclusion in a group calculation (TRUE/FALSE).

False

true

54. MIN and MAX functions can be applied against numeric, character and date data type

(TRUE/FALSE).

true

55. SELECT department_id, COUNT (last_name) FROM employees;

(What is wrong with above statement?)

Single row functions and group fns are used together;

Any coloumn name which is used before group fns that must be included in GROUP BY clause .

56. Write a query to find the highest, lowest, sum, and average salary of all employees. Label the

columns Maximum, Minimum, Sum, and Average, respectively. Round your results to the nearest

whole number.
select job_id,max(salary) as "MAXIMUM",min(salary) as "MINIMUM",sum(salary) as

"SUM",round(avg(salary),0) as "average" from employees group by job_id;

select max(salary) as "MAXIMUM", min(salary) as "MINIMUM",sum(salary) as

"SUM",round(avg(salary)) as "average" from employees;

57. All columns in the SELECT list that are not in GROUP functions must be in the GROUP BY clause

(TRUE/FALSE).

True;

58. Write a Query to display the minimum, maximum, sum, and average salary for each job type.

Select job_id,max(salary),min(salary),sum(salary),avg(salary) from employees group by job_id;

59. SELECT department_id, AVG(salary) FROM employees WHERE AVG(salary) > 8000 GROUP BY

department_id;

(What is wrong with above statement?)

While using group fns instead of where clause,having clause is used;

Group fn can not be used in WHERE clause.

60. Write a query to display the number of people with the same job.

Select count(job_id)-count(distinct(job_id)) from employees;

61. You can use a column alias in the GROUP BY clause (TRUE/FALSE).

False.

62. Write a query to determine the number of managers without listing them. Label the column

Number of Managers.

Select count(distinct(manager_id)) as “Number of Managers” from employees;

Number of Managers

------------------

18

63. Write a query to find the difference between the highest and the lowest salaries. Label the

column DIFFERENCE.

select max(salary)-min(salary) as "difference" from employees;


difference

----------

21900

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy