Questions For SQL Part 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

Questions for SQL Part 1

1. What is the correct SQL statement to retrieve the 5th highest salary in a table?
SELECT DISTINCT salary FROM employees ORDER BY salary DESC OFFSET 4 ROWS
FETCH NEXT 1 ROWS ONLY;
SELECT TOP 5 salary FROM employees ORDER BY salary ASC;
SELECT salary FROM employees ORDER BY salary DESC FETCH FIRST 5 ROWS
ONLY;
SELECT salary FROM employees ORDER BY salary ASC FETCH NEXT 1 ROWS ONLY
OFFSET 4 ROWS;

2. The function that an entity plays in a relationship is called that entity’s _____________
Instance
Role
Position
Participation

3. Identify the output of the following SQL query:


SELECT LPAD ('SQL', 6, '*') FROM dual;
SQL
***SQL
*SQL
SQL

4. Which SQL keyword is used to combine the results of two queries and return only distinct
rows?
INTERSECT;
UNION;
JOIN;
DISTINCT;

5. FIRST_NAMEVARCHAR2 (50)
LAST_NAMENOT NULLVARCHAR2 (50)
ADDRESSVARCHAR2 (50)
CITYVARCHAR2 (25)
STATEVARCHAR2 (3)
You want to display details of all members who reside in states starting with the letter A
followed by exactly one character.
Which SQL statement must you execute?
SELECT * FROM MEMBERS WHERE state LIKE 'A%';
SELECT * FROM MEMBERS WHERE state LIKE '%A_*;
SELECT * FROM MEMBERS WHERE state LIKE 'A_';
SELECT * FROM MEMBERS WHERE state LIKE 'A_%';

6. Identify the output of the following SQL query:


SELECT ROUND (1234.567, -2) from dual;
1234.57
1230
1200
1235

7. What will the following query do?


SELECT * FROM employees WHERE salary BETWEEN 5000 AND 10000;
Throws an error
Retrieves employees with a salary between 5000 and 10000 inclusive
Retrieves employees with a salary greater than 5000
Retrieves employees with a salary less than 10000

8. What is the result of this query?


SELECT department_id, COUNT(*)
FROM employees
GROUP BY department_id
HAVING COUNT(*) > 5;
Average number of employees per department;
Departments with more than 5 employees;
Total number of departments;
Departments with less than 5 employees;

9. In an ER diagram, which of the following is true about the cardinality ratio between
entities?
A many-to-one relationship means each entity in the first set relates to many entities in the
second set;
A one-to-one relationship means every entity in both sets relates to at most one entity in the
other set;
A many-to-many relationship means every entity in both sets is related to exactly one entity
in the other set;
A one-to-many relationship means each entity in the second set relates to many entities in the
first set;

10. Which of the following SQL queries will return the first three characters of FirstName
and the Salary rounded to one decimal place from the Employee table?
SELECT SUBSTRING (FirstName, 1, 3), ROUND (Salary, 0.1) FROM Employee;
SELECT LEFT (FirstName, 3), ROUND(Salary, 1) FROM Employee;
SELECT SUBSTR (FirstName, 3, 1), ROUND (Salary, 1) FROM Employee;
SELECT SUBSTR (FirstName, 1, 3), ROUND (Salary, 1) FROM Employee;
11. Which SQL clause is used to filter the rows retrieved by a SELECT statement?
HAVING
WHERE
GROUP BY
ORDER BY

12. Which of the following clauses is used to filter records after the aggregation using the
GROUP BY clause?
DISTINCT;
ORDER BY;
HAVING;
WHERE;

13. Evaluate this SQL statement:


SELECT TO_CHAR (list_price, '$9,999') From product_information;
Which two statements are true regarding the output?
A row whose LIST_PRICE column contains value 1123.90 would be displayed as $1,124.
A row whose LIST_PRICE column contains value 11235.90 would be displayed as $1,123.
A row whose LIST_PRICE column contains value 1123.90 would be displayed as $1,123.
A row whose LIST_PRICE column contains value 11235.90 would be displayed as #######.

14. What will be the result of the following query?


SELECT employee_name, salary FROM employees WHERE salary > ALL (SELECT salary
FROM employees WHERE department_id = 5);
Employees earning more than the average salary in department 5;
Employees with the highest salary in each department;
Employees earning more than all employees in department 5;
Employees earning less than employees in department 5;

15. Identify the output of the following SQL query:


SELECT NVL2(NULL, 'A', 'B') FROM dual;
NULL
B
A
NULL,B

16. Identify the output of the following SQL query:


SELECT LENGTH('SQL') + LENGTH('Server') FROM dual;
10
9
12
11

17. Identify the output of the following SQL query:


SELECT COALESCE (NULL, '', 'Hello', 'World') FROM dual;
World
'' (empty string)
Hello
NULL

18. What will be the output of the following C code?


SELECT first_name || ' ' || last_name AS full_name FROM employees;
Concatenates first_name and last_name columns with a space in between
Returns first_name and last_name separated by a comma
Throws an error
Returns the last name only

19. Evaluate the following SQL statement:


SELECT product_name || 'it's not available for order' FROM product_information
WHERE product_status = 'obsolete';
You received the following error while executing the above query: ERROR
ORA-01756: quoted string not properly terminated What would you do to execute the query
successfully?
Do not enclose the character literal string in the SELECT clause within the single quotation
marks.
Use Quote (q) operator and delimiter to allow the use of single quotation mark in the literal
character string.
Use escape character to negate the single quotation mark inside the literal character string in
the SELECT clause.
Enclose the literal character string in the SELECT clause within the double quotation marks.

20. What will this query return?


SELECT employee_name
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);
Employees earning more than the average salary;
Employees earning less than the average salary;
Employees earning the average salary;
Employees earning exactly the average salary;
21. What does the following query return?
SELECT INITCAP('oracle sql');
Throws an error
Oracle SQL
Oracle sql
oracle Sql

22. Given the table Employee: Employee(EmpID INT, FirstName VARCHAR(30), Salary
DECIMAL(10, 2)). What will be the output of the following query?
SELECT CONCAT(UPPER(SUBSTR(FirstName, 1, 1)), LOWER(SUBSTR(FirstName,
2))), ROUND(Salary, 0) FROM Employee WHERE EmpID = 200; Assume FirstName is
'jessica' and Salary is 45678.89 for EmpID = 200.
JESSICA | 45679
Jessica | 45679
JESSICA | 45678
Jessica | 45678

23. Identify the output of the following SQL query:


SELECT SUBSTR ('Programming', -3, 2) FROM dual;
in
Error
ng
am

24. What is the difference between HAVING and WHERE clauses?


HAVING is used before aggregation, WHERE is used after
They are interchangeable
HAVING is used to filter rows, WHERE is used to filter groups
WHERE is used before aggregation, HAVING is used after

25. Evaluate the following ALTER TABLE statement:


ALTER TABLE orders
SET UNUSED (order_date); Which statement is true?
After executing the ALTER TABLE command, you can add a new column called
ORDER_DATE to the ORDERS table;
The ORDER_DATE column should be empty for the ALTER TABLE command to execute
succsessfully;
ROLLBACK can be used to get back the ORDER_DATE column in the ORDERS table.;
The DESCRIBE command would still display the ORDER_DATE column.;

26. Identify the output of the following SQL query:


SELECT TO_CHAR (SYSDATE, 'YYYY-MM-DD') FROM dual;
NULL
Current date in 'DD-MM-YYYY' format
Current date in 'YYYY-MM-DD' format
Error

27. Which of the following queries retrieves the employees who were hired most recently?
SELECT employee_name FROM employees WHERE hire_date = (SELECT
MAX(hire_date) FROM employees);
SELECT employee_name FROM employees WHERE hire_date = CURDATE();
SELECT employee_name FROM employees WHERE hire_date = NOW();
SELECT employee_name FROM employees ORDER BY hire_date ASC;

28. Which of the following statements correctly finds the employee with the second-highest
salary?
SELECT salary FROM employees ORDER BY salary DESC LIMIT 1, 1;
SELECT salary FROM employees ORDER BY salary ASC LIMIT 1, 1;
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM
employees);
SELECT MAX(salary) FROM employees;

29. What is the purpose of the DISTINCT keyword in a SELECT statement?


To display only non-null values;
To remove duplicate rows from the result;
To sort the result in descending order;
To group similar rows together;

30. How do you alias a table in an SQL query?


ALIAS
LABEL
AS
RENAME
31. Given the table Customer: Customer (CustomerID INT, FullName VARCHAR (50),
CreditLimit DECIMAL (10, 2)). What will be the output of the following query?
SELECT INITCAP(FullName), ROUND (CreditLimit, 1) FROM Customer WHERE
CustomerID = 400;
Assume FullName is 'michael johnson' and CreditLimit is 25400.789 for CustomerID = 400.
MICHAEL JOHNSON | 25400.8
Michael johnson | 25400.8
Michael Johnson | 25400.7
Michael Johnson | 25400.8

32. What will the following query return?


SELECT employee_name, salary
FROM employees
WHERE salary IN (SELECT MAX(salary) FROM employees GROUP BY department_id);
The employee with the highest salary in the company;
The names and salaries of employees with the highest salary in each department;
All employees earning the same as the department average;
Employees earning more than the average salary in each department;

33. Which SQL function can be used to find the position of a substring within a string?
SUBSTR()
LOCATE()
INSTR()
POSITION()

34. Identify the output of the following SQL query:


SELECT COALESCE (NULL, NULL, 5, NULL, 7);
NULL
7
5
NULL, 5, 7

35. View the Exhibit and examine the structure of the ORDER_ITEMS table. (Choose the
best answer.)

You must select the ORDER_ID of the order that has the highest total value among all the
orders in the ORDER_ITEMS table.
Which query would produce the desired result?
SELECT order_idFROM order_itemsWHERE(unit_price*quantity) =
MAX(unit_price*quantity)GROUP BY order_id);
SELECT order_idFROM order_itemsWHERE(unit_price*quantity) = (SELECT MAX
(SUM(unit_price*quantity)FROM order_items) GROUP BY order_id);
SELECT order_idFROM order_itemsGROUP BY order_idHAVING
SUM(unit_price*quantity) = (SELECT MAX (SUM(unit_price*quantity))FROM
order_items
SELECT order_idFROM order_itemsWHERE (unit_price*quantity) = (SELECT
MAX(unit_price*quantity)FROM order_itemsGROUP BY order_id)

36. Given the table OrderDetails: OrderDetails(OrderID INT, Item VARCHAR(30), Quantity
INT, UnitPrice DECIMAL(8, 2)).What will be the output of the following query?
SELECT LOWER(Item), ROUND(UnitPrice * Quantity, -1) FROM OrderDetails WHERE
OrderID = 501;
Assume Item is 'Refrigerator' with Quantity = 3 and UnitPrice = 749.99 for OrderID = 501.
refrigerator | 2240
refrigerator | 2250.0
REFRIGERATOR | 2250
refrigerator | 2250

37. Which of the following statements retrieves all the rows from a table named employees?
SELECT * FROM employees;
SELECT employees FROM *;
SELECT ALL FROM employees;
SELECT ALL (*) FROM employees;

38. In SQL, what does the ORDER BY clause do?


Groups rows that have the same values;
Specifies the maximum number of rows to return;
Sorts the result set by one or more columns;
Filters rows based on a condition;

39. What is the output of the following query?


SELECT COUNT(DISTINCT department_id) FROM employees;
The total number of distinct departments;
The number of employees in each department;
The sum of all department IDs;
The total number of employees;
40. What does the following SQL query do?
SELECT COUNT(*) FROM employees WHERE department_id = 10;
Counts the number of employees in department 10
Throws an error
Retrieves the rows for department 10
Counts the number of rows in the table employees

41. In the context of an ER model, a "weak entity" can be best described as:?
An entity that lacks a candidate key and must rely on an identifying relationship;
An entity that can exist independently of other entities;
An entity that has a primary key but no foreign key;
An entity with a composite primary key;

42. Which SQL function is used to return the position of the first occurrence of a substring
within a string?
FIND;
POSITION;
LOCATE;
INSTR;

43. How the Every weak entity set can be changed into a strong entity set through?
adding appropriate attributes
none of the above
using aggregation
using generalization

44. How can you select rows that meet at least one of several conditions?
Using OR
Using NOT
Using BETWEEN
Using AND

45. Which SQL clause is used to group rows that have the same values into summary rows?
ORDER BY;
GROUP BY;
HAVING;
WHERE;

46. How do you select rows from a table where a column value is NULL?
SELECT * FROM table WHERE column != NULL;
SELECT * FROM table WHERE column = '';
SELECT * FROM table WHERE column = NULL;
SELECT * FROM table WHERE column IS NULL;

47. What does the following SQL statement do?


SELECT DISTINCT department_id FROM employees;
Throws an error
Filters the result by department ID
Retrieves unique department IDs from the employees table
Retrieves all rows from the employees table

48. Identify the output of the following SQL query:


SELECT INSTR ('Hello world', 'o') FROM dual;
1
3
5
2

49. Which of the following statements about an Entity-Relationship (ER) model is true?
A composite attribute can have multiple values for each entity;
A weak entity set must have a primary key;
A relationship set always contains attributes;
A derived attribute is calculated from other attributes;

50. How do you rename a column in the result set in Oracle SQL?
ALTER
SET
AS
RENAME
51. Which SQL clause is used to group rows with the same values into summary rows?
ORDER BY
WHERE
HAVING
GROUP BY

52. Which operator is used to check for values in a specified list?


EXISTS
IN
LIKE
BETWEEN

53. Identify the output of the following SQL query:


SELECT MOD (10, 3) * FLOOR (15.5) from dual;
0
15
45
9

54. Which keyword is used to eliminate duplicate rows in the result set?
DELETE
DISTINCT
UNIQUE
REMOVE

55. Which of the following defines a "key constraint" in an ER model?


A constraint that limits the number of entities in an entity set;
A constraint that limits the number of relationships a single entity can participate in;
A constraint that defines the uniqueness of an attribute across all instances in an entity set;
A constraint that ensures the foreign key values match primary key values;

56. Which two statements are true regarding the GROUP BY clause in a SQL statement?
(Choose two.)
You can use column alias in the GROUP BY clause.
The GROUP BY clause is mandatory if you are using an aggregate function in the SELECT
clause.
Using the WHERE clause after the GROUP BY clause excludes the rows after creating
groups.
Using the WHERE clause before the GROUP BY clause excludes the rows before creating
groups.

57. Identify the output of the following SQL query:


SELECT COUNT (*), COUNT (1), COUNT(NULL) FROM dual;
NULL, 1, 1
1, 1, 0
1, 1, NULL
1, 1, 1

58. A______is a set of relationships of the same type among several entities
Domain
Value Set
Key set
Relationship

59. Given the table Sales: Sales(SaleID INT, ProductName VARCHAR(50), Quantity INT,
Price DECIMAL(8, 2)). What will be the output of the following query?
SELECT LENGTH(ProductName), ROUND(Price * Quantity, -2) FROM Sales WHERE
SaleID = 301; Assume ProductName is 'Laptop' with Quantity = 5 and Price = 899.99 for
SaleID = 301.
7 | 4500
6 | 45000
6 | 450000
6 | 4500

60. Examine the structure of the BOOKS_TRANSACTIONS table:


You want to display the member IDs, due date, and late fee as $2 for all transactions. Which
SQL statement must you execute?
SELECT member_id AS MEMBER_ID, due_date AS DUE_DATE, $2 AS LATE_FEE
FROM BOOKS_TRANSACTIONS
SELECT member_id AS "MEMBER ID", due_date AS "DUE DATE", $2 AS "LATE FEE"
FROM BOOKS_TRANSACTIONS
SELECT member_id AS "MEMBER ID", due_date AS "DUE DATE", '$2' AS "LATE FEE"
FROM BOOKS_TRANSACTIONS
SELECT member_id 'MEMBER ID', due_date 'DUE DATE', '$2 AS LATE FEE' FROM
BOOKS_TRANSACTIONS;
61. The number of entities to which another entity can be related through a relationship set is
called?
Attributes
Entity
Cardinality
Schema

62. Which of the following represents a correct mapping from an ER diagram to a relational
model?
Each entity set, whether strong or weak, always maps to an individual table;
Each strong entity set maps to a separate table, while weak entity sets map to a table along
with the strong entity's primary key;
Composite attributes are always mapped as separate tables;
Relationship sets never require a separate table;

63. What is the purpose of the LIMIT clause in SQL?;


To limit the number of columns displayed;
To specify the maximum number of rows to return;
To filter rows based on conditions;
To restrict access to specific columns;

64. Which SQL clause is used to specify conditions on aggregated data?


GROUP BY;
ORDER BY;
WHERE;
HAVING;
65. You issued the following command: SQL> DROP TABLE employees; Which three
statements are true?
All indexes and constraints defined on the table being dropped are also dropped.
The employees table can be recovered using the rollback command.
Sequences used in the employees table become invalid.
All uncommitted transactions are committed.

66. Given the table Employee: Employee (EmpID INT, FirstName VARCHAR (30),
LastName VARCHAR (30), Salary DECIMAL (10, 2))
Which query will return the LastName in uppercase and the Salary rounded to the nearest
hundred?
SELECT INITCAP(LastName), ROUND (Salary, -2) FROM Employee;
SELECT LOWER(LastName), ROUND(Salary, 2) FROM Employee;
SELECT UPPER(LastName), ROUND(Salary, 0) FROM Employee;
SELECT UPPER(LastName), ROUND (Salary, -2) FROM Employee;

67. Identify the output of the following SQL query:


SELECT DECODE (2, 1, 'One', 2, 'Two', 3, 'Three', 'Unknown') FROM dual;
Two
Unknown
Three
One

68. Identify the output of the following SQL query:


SELECT SUBSTR ('Database', 5, 3) FROM dual;
Data
tab
base
bas

69. Each student can work on multiple projects and each project can have multiple students.
You need to design an Entity Relationship Model (ERD) for optimal data storage and allow
for generating reports in this format:
STUDENT_ID FIRST_NAME LAST_NAME PROJECT_ID PROJECT_NAME
PROJECT_TASK
Which two statements are true in this scenario?
STUDENT_ID must be the primary key in the STUDENTS entity and foreign key in the
PROJECTS entity.
PROJECT_ID must be the primary key in the PROJECTS entity and foreign key in the
STUDENTS entity
The ERD must have a 1:M relationship between the STUDENTS and PROJECTS entities.
The ERD must have a M:M relationship between the STUDENTS and PROJECTS entities
that must be resolved into 1:M relationships.

70. What does the following query return?


SELECT TRUNC(salary, 1) FROM employees;
Salary rounded to one decimal place
Throws an error
Salary truncated to the nearest integer
Salary truncated to one decimal place
71. How can you limit the number of rows returned by a query in Oracle SQL?
ROWNUM
TOP
OFFSET
LIMIT

72. Examine the following query:


SQL> SELECT prod_id, amount_sold FROM sales
ORDER BY amount_sold
FETCH FIRST 5 PERCENT ROWS ONLY;
What is the output of this query?
It displays 5 percent of the products with the lowest amount sold.
It displays 5 percent of the products with the highest amount sold.
It displays the first 5 percent of the rows from the SALES table.
It results in an error because the ORDER BY clause should be the last clause.

73. The attribute name could be structured as an attribute consisting of first name, middle
initial, and last name. This type of attribute is called
Simple attributeOption
Multivalued attribute
Composite attribute
Derived attribute
74. Which of the following SQL functions can be used to get the current date and time in
Oracle?
GETDATE()
CURRENT_DATE()
SYSDATE()
NOW()

75. Identify the output of the following SQL query:


SELECT LENGTH (TRIM (' Hello World ')) FROM dual;
14
11
16
15

You might also like