This set of MySQL Database Multiple Choice Questions & Answers (MCQs) focuses on “Table Retrievals with Union”.
1. To combine multiple retrievals, we write several SELECT statements and put the keyword between them. What is the keyword?
Explanation: The ‘UNION’ operator is used for combining the results of various ‘SELECT’ queries into one. For example, ‘SELECT a FROM table1 UNION SELECT a FROM table2;’ produces the results from tables table1 concatenated with that of table2.
2. What is ‘xyz’ in the following statement?
SELECT xyz FROM table1 UNION xyz FROM table2;
a) row name
b) column name
c) table name
d) database name
Explanation: The ‘SELECT’ queries can be combined together using the ‘UNION’ operator to produce the concatenated results from two or more tables. The data type of the columns is not taken into account.
3. The following statement is invalid.
SELECT abc, xyz FROM table1 UNION abc, def FROM table2;
Explanation: Even if the columns ‘xyz’ and ‘def’ have different data types, the results from these columns are placed into the column ‘xyz’. The data types can be determined from the values in the columns.
4. Which keyword used with UNION does not retain duplicate rows?
Explanation: The keyword ‘DISTINCT’ used along with ‘UNION’ is synonymous with just the ‘UNION’ statement. It produces only the distinct rows from the combination of the two tables in the SELECT query.
5. Which keyword used with UNION retains duplicate rows?
Explanation: The keyword ‘ALL’ used along with ‘UNION’ is not synonymous with just the ‘UNION’ statement. It produces the duplicate rows, if they exist, from the combination of the two tables in the SELECT query.
6. The UNION ALL has a higher precedence than UNION DISTINCT.
Explanation: When there is a mixture of ‘UNION’ or ‘UNION DISTINCT’ statements with the ‘UNION ALL’ statements, any distinct union operation takes precedence over any ‘UNION ALL’ operations to its left.
7. Which clause is used to sort a UNION result as a whole?
b) ORDER BY
c) GROUP BY
Explanation: The ‘ORDER BY’ clause is used along with the ‘UNION’ statement to sort a ‘UNION’ result as a whole. It is placed after the last ‘SELECT’ statement which is kept in parentheses.
8. Suppose it is desired that UNION operation should return not more than 3 rows. Which keyword is used for this?
Explanation: When there is a need to put a limit to the number of rows returned by the ‘UNION’ operation, the statement ‘LIMIT’ is appended to the ‘SELECT’ queries which are joined by the ‘UNION’ operations.
9. Which table is used to run a UNION-type query on MyISAM tables?
Explanation: When there is a need to run a ‘UNION type’ operation on a MyISAM table that has the same structures, a ‘MERGE’ table is set up for it. After this, the queries are performed on this table.
10. SELECT on a MERGE table is like _____________
a) UNION ALL
c) UNION DISTINCT
Explanation: Performing a ‘SELECT’ operation on a ‘MERGE’ table is like performing ‘UNION ALL’. This means that duplicate row results are not removed. ‘SELECT DISTINCT’ is like ‘UNION’ or ‘UNION DISTINCT’.
Sanfoundry Global Education & Learning Series – MySQL Database.
To practice all areas of MySQL Database, here is complete set of 1000+ Multiple Choice Questions and Answers.