Database Programming With SQL - MidTerm 2019

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

1.

Columns in a database table contain data with the same _________:


 Type (*)
 Row
 Key
 Field

2. What command retrieves data from the database?


 DESCRIBE
 SELECT (*)
 INSERT
 ALTER

3. All computers in the world speak the same languages, so you only need to learn one programming
language - Oracle SQL. True or False?
 True
 False (*)

4. Evaluate this SELECT statement:


SELECT (salary * raise_percent) raise
FROM employees;
If the RAISE_PERCENT column only contains null values, what will the statement return?
 A null value or a zero depending on the value of the SALARY column
 Only zeroes
 A null value or a numeric value depending on the value of the SALARY column
 Only null values (*)

5. What would you use in the SELECT clause to return all the columns in the table?
 An asterisk (*) (*)
 A plus sign (+)
 The ALL keyword
 A minus sign (-)

6. You need to combine the FIRST_NAME and LAST_NAME columns in the EMPLOYEES table and display
the columns as a combined character string. Which operator should you use?
 |
 AND
 || (*)
 +

7. The following is a valid SQL SELECT statement. True or False?


SELECT first_name || ' ' || last_name alias AS Employee_Name
FROM employees:
 True
 False (*)
8. You want to retrieve a list of customers whose last names begin with the letters 'Fr' . Which symbol
should you include in the WHERE clause of your SELECT statement to achieve the desired result?
 *
 ~
 % (*)
 #

9. To restrict the rows returned from an SQL Query, you should use the _____ clause:
 SELECT
 WHERE (*)
 GROUP BY
 CONDITION
 All of the Above

10. The EMPLOYEES table contains these columns:


LAST_NAME VARCHAR2(25)
FIRST_NAME VARCHAR2(25)
EMAIL VARCHAR2(50)
You are writing a SELECT statement to retrieve the names of employees that have an email address.
SELECT last_name||', '||first_name "Employee Name"
FROM employees;
Which WHERE clause should you use to complete this statement?
 WHERE email IS NULL;
 WHERE email = NULL;
 WHERE email != NULL;
 WHERE email IS NOT NULL; (*)

11. If you write queries using the BETWEEN operator, it does not matter in what order you enter the
values, i.e. BETWEEN low value AND high value will give the same result as BETWEEN high value and low
value. True or False?
 True
 False (*)

12. You need to change the default sort order of the ORDER BY clause so that the data is displayed in
reverse alphabetical order. Which keyword should you include in the ORDER BY clause?
 DESC (*)
 ASC
 CHANGE
 SORT

13. The function COUNT is a single row function. True or False?


True
False (*)
14. What value will the following SQL statement return?
SELECT employee_id
FROM employees
WHERE employee_id BETWEEN 100 AND 150
OR employee_id IN(119, 175, 205)
AND (employee_id BETWEEN 150 AND 200);
 100, 101, 102, 103, 104, 107, 124, 141, 142, 143, 144, 149 (*)
 No rows will be returned
 200, 201, 202, 203, 204, 205, 206
 19

15. Evaluate this SELECT statement:


SELECT last_name, first_name, salary
FROM employees;
How will the results of this query be sorted?
 The database will display the rows in whatever order it finds it in the database, so no
particular order. (*)
 The results will be sorted ascending by LAST_NAME and FIRST_NAME only.
 The results will be sorted ascending by the LAST_NAME column only.
 The results will be sorted ascending by LAST_NAME, FIRST_NAME, and SALARY.

16. Round and Trunc cannot be used on Date datatypes. True or False?
 True
 False (*)

17. You issue this SQL statement:


SELECT ROUND (1282.248, -2) FROM dual;
What value does this statement produce?
 1282.25
 1200
 1282
 1300 (*)

18. The PRICE table contains this data:


PRODUCT_ID MANUFACTURER_ID
86950 59604
You query the database and return the value 95. Which script did you use?

SELECT SUBSTR(product_id, 3, 2)
FROM price
WHERE manufacturer_id = 59604; (*)

SELECT SUBSTR(product_id, -1, 3)


FROM price
WHERE manufacturer_id = 59604;
SELECT LENGTH(product_id, 3, 2)
FROM price
WHERE manufacturer_id = 59604;

SELECT TRIM(product_id, -3, 2)


FROM price
WHERE manufacturer_id = 59604;

19. What does the following SQL SELECT statement return?


SELECT UPPER( SUBSTR('Database Programming', INSTR('Database Programming','P'),20))
FROM dual;
 Database
 PROGRAMMING (*)
 DATABASE
 Programming

20. Which statement about group functions is true?


 COALESCE, but not NVL and NVL2, can be used with group functions to replace null
values.
 NVL and NVL2, but not COALESCE, can be used with group functions to replace null
values.
 NVL and COALESCE, but not NVL2, can be used with group functions to replace null
values.
 NVL, NVL2, and COALESCE can be used with group functions to replace null values. (*)

21. The STYLES table contains this data:

Evaluate this SELECT statement:

SELECT style_id, style_name, category, cost


FROM styles
WHERE style_name LIKE 'SANDAL' AND NVL(cost, 0) < 15.00
ORDER BY category, cost;

Which result will the query provide?


STYLE_ID STYLE_NAME CATEGORY COST
968950 SANDAL 85909 10.00
895840 SANDAL 85940 12.00
758960 SANDAL 86979

22. Which statement will return a listing of last names, salaries, and a rating of 'Low', 'Medium', 'Good'
or 'Excellent' depending on the salary value?

SELECT last_name,salary,
(RATING WHEN salary<5000 THEN 'Low'
WHEN salary<10000 THEN 'Medium'
WHEN salary<20000 THEN 'Good'
ELSE 'Excellent'
END) qualified_salary
FROM employees;

SELECT last_name,salary,
(CASE WHEN salary<5000 THEN 'Low'
WHEN salary<10000 THEN 'Medium'
WHEN salary<20000 THEN 'Good'
ELSE 'Excellent'
END) qualified_salary
FROM employees; (*)

SELECT last_name,sal,
(CASE WHEN sal<5000 THEN 'Low'
WHEN sal<10000 THEN 'Medium'
WHEN sal<20000 THEN 'Good'
ELSE 'Excellent'
END) qualified_salary
FROM employees;

SELECT last_name,salary,
(CASE WHEN salary<5000 THEN 'Low'
WHEN sal <10000 THEN 'Medium'
WHEN sal <20000 THEN 'Good'
ELSE 'Excellent'
END) qualified_salary
FROM employees;

23. Which SQL Statement should you use to display the prices in this format: "$00.30"?
 SELECT TO_CHAR(price, '$99,999.99') FROM product;
 SELECT TO_NUMBER(price, '$99,900.99') FROM product;
 SELECT TO_CHAR(price, '$99,990.99') FROM product;
 SELECT TO_CHAR(price, '$99,900.99') FROM product; (*)
24. You need to display the HIRE_DATE values in this format: 25th of July 2002. Which SELECT statement
would you use?
 SELECT TO_CHAR(hire_date, 'DDspth 'of' Month RRRR') FROM employees;

 SELECT TO_CHAR(hire_date, 'DDTH "of" Month YYYY') FROM employees;


 SELECT enroll_date(hire_date, 'DDspth "of" Month YYYY') FROM employees;
 SELECT TO_CHAR(hire_date, 'ddth "of" Month YYYY') FROM employees; (*)

25. All Human Resources data is stored in a table named EMPLOYEES. You have been asked to create a
report that displays each employee's name and salary. Each employee's salary must be displayed in the
following format: $000,000.00. Which function should you include in a SELECT statement to achieve the
desired result?
 TO_NUMBER
 TO_DATE
 TO_CHAR (*)
 CHARTOROWID

26. Which SELECT statement implements a self join?

SELECT e.employee_id, m.manager_id


FROM employees e, departments m
WHERE e.employee_id = m.manager_id;

SELECT e.employee_id, m.manager_id


FROM employees e, managers m
WHERE e.employee_id = m.manager_id;

SELECT e.employee_id, m.manager_id


FROM employees e, employees m
WHERE m.employee_id = e.manager_id; (*)

SELECT e.employee_id, m.manager_id


FROM employees e
NATURAL JOIN employees m;

27. Which statement about a self join is true?


 A self join must be implemented by defining a view.
 Table aliases must be used to qualify table names. (*)
 The NATURAL JOIN clause must be used.
 Table aliases cannot be used to qualify table names.

28. You need to join all the rows in the EMPLOYEES table to all the rows in the EMP_REFERENCE table.
Which type of join should you create?
 A full outer join
 A cross join (*)
 An inner join
 An equijoin
29. The join column must be included in the select statement when you use the NATURAL JOIN clause.
True or False?
 True
 False (*)

30. You need to display all the rows (both matching and non-matching) from both the EMPLOYEE and
EMPLOYEE_HIST tables. Which type of join would you use?
 A right outer join
 A left outer join
 An inner join
 A full outer join (*)

31. EMPLOYEES Table:

A query is needed to display each department and its manager name from the above tables. However,
not all departments have a manager but we want departments returned in all cases. Which of the
following SQL: 1999 syntax scripts will accomplish the task?

SELECT d.department_id, e.first_name, e.last_name FROM employees e


FULL OUTER JOIN departments d
ON (e.employee_id = d.manager_id);

SELECT d.department_id, e.first_name, e.last_name FROM employees e, departments d


WHERE e.employee_id
RIGHT OUTER JOIN d.manager_id;

SELECT d.department_id, e.first_name, e.last_name FROM employees e


LEFT OUTER JOIN departments d
WHERE (e.department_id = d.department_id);

SELECT d.department_id, e.first_name, e.last_name FROM employees e


RIGHT OUTER JOIN departments d
ON (e.employee_id = d.manager_id); (*)
32. The keywords JOIN _____________ should be used to join tables with the same column names but
different datatypes.
 WHEN
 USING (*)
 NATURAL ON
 ON

33. Which statement about outer joins is true?


 The tables must be aliased.
 Outer joins are always evaluated before other types of joins in the query.
 The OR operator cannot be used to link outer join conditions. (*)
 The FULL, RIGHT, or LEFT keyword must be included.

34. You need to create a report that lists all employees in department 10 (Sales) whose salary is not
equal to $25,000 per year. Which query should you issue to accomplish this task?

SELECT last_name, first_name, salary


FROM employees
WHERE salary > 25000 AND dept_id = 10;

SELECT last_name, first_name, salary


FROM employees
WHERE salary <= 25000 AND dept_id = 10;

SELECT last_name, first_name, salary


FROM employees
WHERE salary != 25000 AND dept_id = 10; (*)

SELECT last_name, first_name, salary


FROM employees
WHERE salary = 25000 AND dept_id = 10;

35. What happens when you create a Cartesian product?


 The table is joined to another equal table
 All rows that do not match in the WHERE clause are displayed
 All rows from one table are joined to all rows of another table (*)
 The table is joined to itself, one column to the next column, exhausting all possibilities

36. Which SELECT statement will calculate the number of rows in the PRODUCTS table?
 SELECT COUNT FROM products;
 SELECT COUNT(products);
 SELECT ROWCOUNT FROM products;
 SELECT COUNT (*) FROM products; (*)
37. You need to compute the total salary amount for all employees in department 10. Which group
function will you use?
 MAX
 COUNT
 VARIANCE
 SUM (*)

38. Which group functions below act on character, number, and date data types? (Choose all that are
correct.)
 AVG
 SUM
 MAX (*)
 COUNT (*)
 MIN (*)

39. If you want to include subtotals and grand totals for all columns mentioned in a GROUP BY clause,
you should use which of the following extensions to the GROUP BY clause?
 HAVING
 GROUP BY ALL COLUMNS
 CUBE (*)
 ROLLUP

40. CUBE can be applied to all aggregate functions including AVG, SUM, MIN, MAX, and COUNT. True or
False?
 True (*)
 False

41. The ___________ operator returns all rows from both tables, after eliminating duplicates.
 UNION (*)
 INTERSECT
 MINUS
 UNION ALL
42. You want to write a report that returns the average salary of all employees in the company, sorted by
departments.
The EMPLOYEES table contains the following columns:
EMPLOYEES:
EMP_ID NUMBER(10) PRIMARY KEY
LNAME VARCHAR2(20)
FNAME VARCHAR2(20)
DEPT VARCHAR2(20)
HIRE_DATE DATE
SALARY NUMBER(10)
Which SELECT statement will return the information that you require?
 SELECT salary(AVG), dept FROM employees GROUP BY dept;
 SELECT dept, AVG(salary) FROM employees GROUP BY dept; (*)
 SELECT AVG salary FROM employees BY dept;
 SELECT AVG (salary) FROM employees BY dept;

43. What is the correct order of clauses in a SELECT statement?

SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY (*)

SELECT
FROM
WHERE
HAVING
ORDER BY
GROUP BY

SELECT
FROM
HAVING
GROUP BY
WHERE
ORDER BY

SELECT
FROM
WHERE
ORDER BY
GROUP BY
HAVING
44. Is the following statement correct?
SELECT department_id, AVG(salary)
FROM employees;
 Yes
 No, because the AVG function cannot be used on the salary column
 No, because a GROUP BY department_id clause is needed (*)
 Yes, because the SELECT clause can contain both individual columns and group functions

45. You need to display all the players whose salaries are greater than or equal to John Brown's salary.
Which comparison operator should you use?
 =
 >= (*)
 >
 <=

46. Oracle allows you to write named subqueries in one single statement, as long as you start your
statement with the keyword WITH. True or False?
 True (*)
 False

47. Examine the structures of the PARTS and MANUFACTURERS tables:


PARTS:
PARTS_ID VARCHAR2(25) PK
PARTS_NAME VARCHAR2(50)
MANUFACTURERS_ID NUMBER
COST NUMBER(5,2)
PRICE NUMBER(5,2)

MANUFACTURERS:
ID NUMBER PK
NAME VARCHAR2(30)
LOCATION VARCHAR2(20)

Assume that the tables have been populated with data including 100 rows in the PARTS table, and 20
rows in the MANUFACTURERS table. Which SQL statement correctly uses a subquery?

SELECT parts_name, price, cost


FROM parts
WHERE manufacturers_id IN
(SELECT id
FROM manufacturers m
JOIN parts p
ON (m.id = p.manufacturers_id)); (*)
UPDATE parts SET price = price * 1.15
WHERE manufacturers_id =
(SELECT id
FROM manufacturers
WHERE UPPER(location) IN("ATLANTA", "BOSTON", "DALLAS"));

SELECT parts_name, price, cost


FROM parts
WHERE manufacturers_id !=
(SELECT id
FROM manufacturers
WHERE LOWER(name) = 'cost plus');

SELECT parts_name
FROM (SELECT AVG(cost) FROM manufacturers)
WHERE cost > AVG(cost);

48. Group functions can be used in subqueries even though they may return many rows. True or False?

 True (*)
 False

49. Evaluate this SELECT statement:

SELECT player_id, name


FROM players
WHERE team_id IN
(SELECT team_id
FROM teams
WHERE team_id > 300 AND salary_cap > 400000);

What would happen if the inner query returned a NULL value?


 A syntax error in the inner query would be returned.
 A syntax error in the outer query would be returned.
 No rows would be returned by the outer query. (*)
 All the rows in the PLAYER table would be returned by the outer query.

50. Single row subqueries may not include this operator:


 <>
 ALL (*)
 >
 =

You might also like