Challenge your understanding of MySQL, covering foundational commands and advanced database features. This quiz explores topics like data types, indexing, joins, transactions, functions, performance tuning, and query optimization for comprehensive learning.
Which SQL statement correctly sets the 'id' column as the primary key when creating a 'users' table?
Explanation: The correct option specifies 'id INT PRIMARY KEY' within the column definition, correctly making 'id' the primary key. The second option uses incorrect syntax for specifying a primary key. The third option incorrectly places 'PRIMARY KEY' outside the proper context. The fourth option places 'PRIMARY KEY' next to 'VARCHAR(50)', which would make 'name' the key, not 'id', and the syntax is incorrect.
What data type is most appropriate for storing prices such as 19.99 and 101.95 in MySQL?
Explanation: DECIMAL is the correct data type as it is designed for fixed-point numbers like monetary values to prevent rounding errors. VARCHAR is for text data and unsuitable for calculations. INT cannot store fractional values as it only supports integers. BINARY is used for binary data such as images or files, not numbers.
Suppose you want the 'customer_id' to be generated automatically with each new row added to a table. Which attribute should be used in the column definition?
Explanation: AUTO_INCREMENT ensures the database generates a unique sequential value for each new row, which is commonly used for primary keys. UPDATE_NULL is not a valid MySQL keyword. SERIALIZE and AUTOFILL are not MySQL attributes for handling incrementing values.
What is the correct SQL statement to retrieve all columns from a table named 'orders'?
Explanation: SELECT * FROM orders; is the correct syntax to select all columns from the 'orders' table. The second option has incorrect word order. 'GET ALL FROM orders;' is not valid SQL and 'EXTRACT * FROM orders;' uses an invalid SQL command.
Which query finds users with an email ending in '.com' in the 'users' table?
Explanation: Using the LIKE operator with the pattern '%.com' correctly matches any email ending in '.com'. The second option only matches exact emails equal to '.com', which is unlikely. 'HAS' and 'ENDSWITH' are not valid MySQL operators for this purpose.
Which JOIN retrieves all records from the left table and matching records from the right table, filling with NULL if no match exists?
Explanation: LEFT JOIN returns all records from the left table and matches from the right, padding with NULL where there is no match. INNER JOIN only returns matched records. RIGHT JOIN does the opposite, returning all records from the right table. CROSS JOIN returns a cartesian product of both tables, not based on matches.
If you often search the 'customers' table by 'email', which statement adds an index for faster queries?
Explanation: CREATE INDEX idx_email ON customers (email); is the correct syntax for adding an index to the 'email' column. The second, third, and fourth options are either not valid commands or use incorrect syntax not recognized by MySQL.
Which statement guarantees a set of SQL operations are all completed successfully or rolled back if any fail?
Explanation: Using BEGIN; ... COMMIT; wraps operations in a transaction, ensuring atomic execution or rollback upon error. MERGE ALL is not a transaction command. ALTER SEQUENCE is for changing sequence objects, not transactions. REPEAT UNTIL SUCCESS; is not a recognized statement in MySQL.
Which SQL clause is used to group results by a specific column, such as city names in an 'addresses' table?
Explanation: GROUP BY is the correct SQL clause for grouping records that have the same values in specified columns. ORDER BY arranges row order but does not group them. CLASSIFY BY and BUNDLE BY are not valid SQL clauses.
How would you modify a query to retrieve only the first 5 rows from a 'products' table?
Explanation: The LIMIT clause restricts the number of rows returned, making 'SELECT * FROM products LIMIT 5;' correct. SELECT TOP 5 is not standard MySQL syntax. The THIRD and fourth options use incorrect SQL or non-existent clauses.
To enforce a relationship where each order in 'orders' is linked to an entry in 'customers', how do you declare a foreign key?
Explanation: FOREIGN KEY (customer_id) REFERENCES customers(id) is the correct syntax for establishing referential integrity. The other options use incorrect or non-existent SQL keywords for creating a foreign key.
When searching for partial matches of the name 'Ann' in a 'people' table, which operator should be used?
Explanation: LIKE supports wildcards for partial string matching, making it suitable for this case. EQUALS only works for complete, exact matches. MATCHES and FIND are not valid operators in standard MySQL SQL.
Which SQL function calculates the total number of rows in a table regardless of NULL values?
Explanation: COUNT(*) counts all rows, including those with NULL values, making it the correct choice. SUM(*) is not a valid syntax and SUM is used for adding column values. TOTAL() and ROWS() are not standard MySQL functions.
Which SQL condition can be used to specifically select records where the 'deleted_at' column has no value?
Explanation: 'IS NULL' correctly checks for missing or NULL values in a column. Using '=' for NULL comparisons is invalid in SQL logic. IS BLANK and EQUALS NULL do not exist as valid conditional checks in MySQL.
Which SQL statement finds products with a price greater than the average price in the 'products' table?
Explanation: The correct query uses a subquery to calculate the average and compares each price against it. HAVING is for filtering after grouping, not suitable here. MEAN and ALL(AVG(price)) are invalid or misuse functions in standard SQL.
Which approach commonly improves query speed when filtering by 'username' in a large 'users' table?
Explanation: Adding an index enables faster searches by reducing the need to scan every row. Storing data as JSON can complicate and slow down filtering. Using DELETE is irrelevant for data retrieval, and increasing VARCHAR size affects storage, not lookup performance.