SQL Query Interview Questions And Answers
List of most frequently asked SQL Query Interview Questions And Answers. Questions from basics to advance level with the solution.
1. What does SQL stand for?
Structured Query Language
2. How do you select all records from the table?
Select * from table_name;
3. What is a join?
Join is a process of retrieve pieces of data from different sets (tables) and returns them to the user or program as one joined collection of data.
4. How do you add a record to a table?
INSERT into table_name VALUES (… , …);
5. How do you add a column to a table?
ALTER TABLE Department ADD (AGE, NUMBER);
6. How do you change the value of the field?
UPDATE EMP_table
set number = 200 where item_munber = ....;
update name_table set status = 'enable' where phone = '4161112222';
update SERVICE_table set REQUEST_DATE = to_date ('2006-03-04 09:29', 'yyyy-mm-dd hh24:MI') where phone = '4161112222';
7. What does COMMIT do?
Saves all changes made by DML statements.
8. What is a primary key?
The column (columns) that has completely unique data throughout the table is known as the primary key field.
9. What are foreign keys?
A foreign key field is a field that links one table to another table’s primary or foreign key.
10. What is the main role of a primary key in a table?
The main role of a primary key in a data table is to maintain the internal integrity of a data table.
11. Can a table have more than one foreign key defined?
A table can have any number of foreign keys defined. It can have only one primary key defined.
List all the possible values that can be stored in a BOOLEAN data field.
There are only two values that can be stored in a BOOLEAN data field:
-1(true) and 0(false).
12. What is the highest value that can be stored in a BYTE data field?
The highest value that can be stored in a BYTE field is 255. or from -128 to 127. A byte is a set of Bits that represent a single character. Usually, there are 8 Bits in a Byte, sometimes more, depending on how the measurement is being made. Each Char requires one byte of memory and can have a value from 0 to 255 (or 0 to 11111111 in binary).
13. How many places to the right of the decimal can be stored in a CURRENCY data field?
The CURRENCY data type can store up to four places to the right of the decimal. Any data beyond fourth place will be truncated by Visual Basic without reporting an error.
14. What is a stored procedure?
A procedure is a group of PL/SQL statements that can be called by a name. Procedures do not return values they perform tasks.
15. Describe how NULLs work in SQL?
The NULL is how SQL handles missing values. Arithmetic operation with NULL in SQL will return a NULL.
16. What is Normalization?
The process of table design is called normalization.
17. What are referential integrity constraints?
Referential integrity constraints are rules that are part of the table in a database schema.
18. What is a Trigger?
The trigger will execute a block of procedural code against the database when a tabled event occurs. A trigger defines a set of actions that are performed in response to an insert, update, or delete operation on a specified table. When such an SQL operation is executed, in this case, the trigger has been activated.
19. Which of the following WHERE clauses will return only rows that have a NULL in the PerDiemExpenses column?
A) WHERE PerDiemExpenses <>
B) WHERE PerDiemExpenses IS NULL
C) WHERE PerDiemExpenses = NULL
D) WHERE PerDiemExpenses NOT IN (*)
B is correct When searching for a NULL value in a column, you must use the keyword IS. No quotes are required around the keyword NULL.
20. You issue the following query: SELECT FirstName FROM StaffListWHERE FirstName LIKE’_A%’Which names would be returned by this query? Choose all that apply.
A) Allen
B) CLARK
C) JACKSON
D) David
C is correct Two wildcards are used with the LIKE operator. The underscore (_) stands for any one character of any case, and the percent sign (%) stands for any number of characters of any case including none. Because this string starts with an underscore rather than a percent sign, it won’t return Allen or Clark because they represent zero and two characters before the “A”. If the LIKE string had been “%A%”, both of these values would have been returned. David was not returned because all non-wildcard characters are case-sensitive. Therefore, only strings with an uppercase “A” as their second letter are returned.
21. Write a SQL SELECT query that only returns each city only once from the Students table? Do you need to order this list with an ORDER BY clause?
SELECT DISTINCT City FROM Students;
The Distinct keyword automatically sorts all data in ascending order. However, if you want the data sorted in descending order, you have to use an ORDER BY clause
22. Write a SQL SELECT sample of the concatenation operator.
SELECT LastName ||',' || FirstName, City FROM Students;
23. How to rename a column in the SQL SELECT query?
SELECT LastName ||',' || FirstName AS "Student Name", City AS "Home City" "FROM StudentsORDER BY "Student Name"
24. Write SQL SELECT example of how you limiting the rows returned with a WHERE clause.
SELECT InstructorID, Salary FROM Instructors WHERE Salary > 5400 AND Salary < 6600;
25. Write SQL SELECT query that returns the first and last name of each instructor, the Salary, and gives each of them a number.
SELECT FirstName, LastName, Salary, ROWNUM FROM Instructors;
26. Which of the following functions can be used only with numeric values?
(Choose all that apply.)
A) AVG
B) MIN
C) LENGTH
D) SUM
E) ROUND
A and D, Only A and D are correct. The MIN function works with any character, numeric, or date datatype. The LENGTH function is a character function that returns the number of letters in a character value. The ROUND function works with both numeric and date values.
27. Which function do you use to remove all padded characters to the right of a character value in a column with a char datatype?
A) RTRIM
B) RPAD
C) TRIM
C is correct. The TRIM function is used to remove padded spaces. LTRIM and RTRIM functions were included in earlier versions of Oracle, but Oracle 8i has replaced them with a single TRIM function
28. Which statement do you use to eliminate padded spaces between the month and day values in a function TO_CHAR(SYSDATE,’ Month, DD, YYYY’)?
To remove padded spaces, you use the “fm” prefix before the date element that contains the spaces.
TO_CHAR(SYSDATE,'fmMonth DD, YYYY')
29. Is the WHERE clause must appear always before the GROUP BY clause in SQL SELECT?
Yes.
The proper order for SQL SELECT clauses is: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY. Only the SELECT and FROM clause are mandatory.
30. How Oracle executes a statement with nested subqueries?
When Oracle executes a statement with nested subqueries, it always executes the innermost query first. This query passes its results to the next query and so on until it reaches the outermost query. It is the outermost query that returns a result set.
31. Which operator do you use to return all of the rows from one query except rows are returned in a second query?
You use the MINUS operator to return all rows from one query except where duplicate rows are found in a second query. The UNION operator returns all rows from both queries minus duplicates.
The UNION ALL operator returns all rows from both queries including duplicates. The INTERSECT operator returns only those rows that exist in both queries.
32. How you will create a column alias? (Oracle 8i)
The AS keyword is optional when specifying a column alias. You must enclose the column alias in double-quotes when the alias contains a space or lowercase letters. If you specify an alias in lowercase letters without double quotes, the alias will appear in uppercase.
33. Which of the following statements are Data Manipulation Language commands?
A) INSERT
B) UPDATE
C) GRANT
D) TRUNCATE
E) CREATE
A and B are correct. The INSERT and UPDATE statements are Data Manipulation Language (DML) commands. GRANT is a Data Control Language (DCL) command. TRUNCATE and CREATE are Data Definition Language (DDL) commands
34. What is Oracle locking?
Oracle uses locking mechanisms to protect data from being destroyed by concurrent transactions.
35. What Oracle lock modes do you know?
Oracle has two lock modes: shared or exclusive. Shared locks are set on database resources so that many transactions can access the resource. Exclusive locks are set on resources that ensure
one transaction has exclusive access to the database resource.
36. What is query optimization?
Query optimization is the part of the query process in which the database system compares different query strategies and chooses the one with the least expected cost.
37. What are the main components of Database management systems software.
The database management system software includes components for storage management, concurrency control, transaction processing, database manipulation interface, database definition interface,and database control interface.
38. What are the main attributes of a database management system?
A database management system is composed of five elements: computer hardware, software, data, people (users), and operations procedures.
39. What is a transaction?
A transaction is a collection of application code and database manipulation code bound into an indivisible unit of execution.it consists from:
BEGIN-TRANSACTION Name
Code
END TRANSACTION Name
40. What databases do you know?
Informix
DB2
SQL
Oracle
41. Describe some Conversion Functions that you know.
TO_CHAR converts a number/date to a string.
TO_DATE converts a string (representing a date) to a date.
TO_NUMBER converts a character string containing digits to a numeric data type, it accepts one parameter which is a column value or a string literal
42. Describe some Group Functions that you know.
1) The COUNT function tells you how many rows were in the result set.
SELECT COUNT(*) FROM TESTING.QA
2) The AVG function tells you the average value of a numeric column.
SELECT MAX(SALARY) FROM TESTING.QA
3) The MAX and MIN functions tell you the maximum and minimum value of a numeric column.
SELECT MIN(SALARY) FROM TESTING.QA
4) The SUM function tells you the sum value of a numeric column.
SELECT SUM(SALARY) FROM TESTING.QA
43. What does DML stand for?
DML is a Data Manipulation Language statement. (SELECT)
44. What does DDL stand for?
DDL is a Data Definition Language statement. (CREATE)
45. What does DCL stand for?
DCL is Data Control Language statements. (COMMIT)
46. Describe SQL comments.
SQL comments are introduced by two consecutive hyphens (–) and ended by the end of the line.
47. In what sequence SQL statements is processed?
The clauses of the subselect are processed in the following sequence (DB2):
1. FROM clause
2. WHERE clause
3. GROUP BY clause
4. HAVING clause
5. SELECT clause
6. ORDER BY clause
7. FETCH FIRST clause
Above are the most common SQL Query Interview Questions And Answers, also visit SQL Interview Questions For Experienced Professionals.