MySQL Database Questions with Answers

14 practice questions on MySQL Database (PHP) with answers and step-by-step solutions. Try 10 samples below, then practise all 14 free in the AptiNest app.

Sample questions

Question 1Medium

The ....... statement is used to delete a table.

  1. DROP TABLE
  2. DELETE TABLE
  3. DEL TABLE
  4. REMOVE TABLE

Answer: A. DROP TABLE

Explanation

If you drop a table, all the rows in the table is deleted and the table structure is removed from the database. Once a table is dropped we cannot get it back, so be careful while using DROP command.

Step-by-step solution

  1. DROP TABLE removes both data and structure from the database.
  2. DELETE TABLE only deletes data but keeps the table structure.
  3. DEL TABLE is not a valid SQL command for deleting tables.
  4. REMOVE TABLE is also not a valid SQL command for deleting tables.
  5. Therefore, DROP TABLE is the correct statement to delete a table.

Why the other options are wrong

  • B. DELETE TABLE is not valid SQL; DELETE FROM removes rows but keeps the table.
  • C. DEL TABLE is not a valid SQL statement for deleting a table.
  • D. REMOVE TABLE is not a valid SQL statement for deleting a table.

Tip: Use DROP TABLE to permanently delete a table and all its data.

Question 2Medium

What does the DESC keyword do in the following query?

SELECT *
FROM MY_TABLE
WHERE ID > 0
ORDER BY ID, NAME DESC

  1. It causes the dataset returned by the query to be sorted in descending order
  2. It causes rows with the same ID to be sorted by NAME in ascending order
  3. It causes rows with the same ID to be sorted by NAME in descending order
  4. It causes rows to be sorted by NAME first and then by ID

Answer: C. It causes rows with the same ID to be sorted by NAME in descending order

Explanation

The DESC keyword is used to sort the fields of a set. The DESC keyword is used in conjunction with the ORDER BY expression in a SELECT statement.

Step-by-step solution

  1. ORDER BY ID, NAME DESC means sorting by ID first then NAME.
  2. DESC applies only to NAME, so same ID rows sort by NAME descending.
  3. ID is not followed by DESC, so it remains in default ascending order.
  4. Thus, rows with same ID are sorted by NAME in descending order.
  5. Final answer: It causes rows with the same ID to be sorted by NAME in descending order.

Why the other options are wrong

  • A. DESC applies only to NAME, so the whole result is not sorted in descending order.
  • B. Rows with the same ID are sorted by NAME descending, not ascending, because DESC applies to NAME.
  • D. NAME is not sorted before ID; ID is the first sort key in this ORDER BY clause.

Tip: DESC affects only the column it directly follows in ORDER BY.

Question 3Medium

In PHP in order to access MySQL database you will use:

  1. mysqlconnect() function
  2. mysql-connect() function
  3. mysql_connect() function
  4. sql_connect() function

Answer: C. mysql_connect() function

Explanation

The function to connect to MySQL is called mysql_connect. This function returns a resource which is a pointer to the database connection. It's also called a database handle.
e.g.

< ?php
$username = "your_name";
$password = "your_password";
$hostname = "localhost";

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
  or die("Unable to connect to MySQL");
echo "Connected to MySQL
";
? >

Step-by-step solution

  1. The question asks for the function used to connect to MySQL with the classic PHP extension.
  2. Mysql_connect takes the host, user name and password, and returns a connection handle.
  3. The correct answer is option C, mysql_connect.

Why the other options are wrong

  • A. Mysqlconnect is missing the underscore, so it is not the function name.
  • B. Mysql-connect uses a hyphen, which is not valid in a PHP function name.
  • D. Sql_connect is not the MySQL connect function.

Tip: PHP function names use underscores, not hyphens.

Question 4Medium

Which of the following is not an SQL aggregate function?

  1. AVG
  2. SUM
  3. MIN
  4. CURRENT_DATE()

Answer: D. CURRENT_DATE()

Explanation

Aggregate functions perform a calculation on a set of values and return a single value. Except for COUNT, aggregate functions ignore null values. current_date() is not sql aggregate function. current_date() Returns the current date. This method always returns the same value within a transaction.

Step-by-step solution

  1. Aggregate functions take a set of values and return one value.
  2. AVG, SUM and MIN all work this way over many rows.
  3. CURRENT_DATE() returns the current date and is not an aggregate, so option D is the answer.

Why the other options are wrong

  • A. AVG works on many values and returns one average, so it is an aggregate.
  • B. SUM adds many values into one total, so it is an aggregate.
  • C. MIN returns the smallest of many values, so it is an aggregate.

Tip: Aggregate functions work over a group of rows and return one value; date functions do not.

Question 5Medium

What will happen at the end of the following sequence of SQL commands?

BEGIN TRANSACTION
DELETE FROM MYTABLE WHERE ID=1
DELETE FROM OTHERTABLE
ROLLBACK TRANSACTION
  1. The contents of OTHERTABLE will be deleted
  2. The contents of both OTHERTABLE and MYTABLE will be deleted
  3. The database will remain unchanged to all users except the one that executes these queries.
  4. The database will remain unchanged

Answer: D. The database will remain unchanged

Explanation

Given that this set of queries is contained within a transaction and that the transaction is rolled back at the end, no changes will be made to the database. Therefore, Answer D is correct.

Step-by-step solution

  1. BEGIN TRANSACTION starts a new transaction block.
  2. DELETE FROM MYTABLE WHERE ID=1 removes row with ID 1 from MYTABLE.
  3. DELETE FROM OTHERTABLE removes all rows from OTHERTABLE.
  4. ROLLBACK TRANSACTION undoes all changes made in the transaction.
  5. Final state of database is same as before commands.

Why the other options are wrong

  • A. OTHERTABLE is not changed, because the transaction is rolled back and the delete is undone.
  • B. Neither table is changed after the rollback; both deletes are undone.
  • C. The database is not changed only for one user; the rollback undoes the changes for everyone.

Tip: Remember: ROLLBACK reverts all changes made during a transaction.

Question 6Medium

Transactions are used to treat sets of SQL statements atomically.

  1. True
  2. False

Answer: A. True

Explanation

A database transaction is a larger unit that frames multiple SQL statements. A transaction ensures that the action of the framed statements is atomic with respect to recovery.

Step-by-step solution

  1. Transactions group several SQL statements into one unit.
  2. A transaction is atomic, so its statements take effect together or not at all.
  3. The statement is true, so the correct answer is option A, True.

Why the other options are wrong

  • B. False is wrong; a transaction treats its framed statements as one atomic unit.

Tip: Atomic means all or nothing.

Question 7Medium

Can joins be nested?

  1. Yes
  2. No

Answer: A. Yes

Explanation

Yes. You can nest an arbitrary number of join clauses, although the results may not always be what you expect.

Step-by-step solution

  1. The question asks whether joins can be nested.
  2. The explanation says an arbitrary number of join clauses can be nested.
  3. So option A, Yes, is the answer.

Why the other options are wrong

  • B. Nesting joins is allowed, so No is wrong.

Tip: Nested joins are allowed, but check the results, since they can be unexpected.

Question 8Medium

Which of the following DBMSs do not have a native PHP extension?

  1. MySQL
  2. PostgreSQL
  3. Microsoft SQL Server
  4. None of the above

Answer: D. None of the above

Explanation

PHP has dedicated extension for MySQL and postgreSQL. While DB2 can be accesed through ODBC and Microsoft sql server using TDS and mssql extension.

Step-by-step solution

  1. The question asks which listed database system has no native PHP extension.
  2. PHP has a dedicated extension for MySQL and one for PostgreSQL.
  3. Microsoft SQL Server also has PHP support through its own extension, so every listed system is covered.
  4. The correct answer is option D, None of the above.

Why the other options are wrong

  • A. MySQL has a dedicated PHP extension.
  • B. PostgreSQL has a dedicated PHP extension.
  • C. Microsoft SQL Server has PHP support through its own extension.

Tip: For which one is missing questions, check each option for its own support before choosing none.

Question 9Medium

Use the ... to delete the data inside the table, and not the table itself?

  1. DROP TABLE
  2. DELETE TABLE
  3. TRUNCATE TABLE
  4. REMOVE TABLE

Answer: C. TRUNCATE TABLE

Explanation

Sometimes we wish to get rid of all the data in a table. One way of doing this is with DROP TABLE. But what if we wish to simply delete the data but not the table itself; For this, we can use the TRUNCATE TABLE command.
The syntax for TRUNCATE TABLE is,

TRUNCATE TABLE "table_name";

Step-by-step solution

  1. TRUNCATE TABLE removes all rows from a table.
  2. It keeps the table structure intact.
  3. DELETE TABLE is not a valid SQL command.
  4. TRUNCATE TABLE is faster than DELETE TABLE.
  5. The correct answer is TRUNCATE TABLE.

Why the other options are wrong

  • A. DROP TABLE removes the table structure as well as the data, not only the rows.
  • B. DELETE TABLE is not valid SQL; DELETE FROM removes rows from a table.
  • D. REMOVE TABLE is not a standard SQL command for removing table data.

Tip: Use TRUNCATE TABLE to clear data without deleting the table.

Question 10Medium

What does an 'inner join' construct do?

  1. It joins two tables together into a third permanent table based on a common column
  2. It creates a result set based on the rows in common between two tables
  3. It creates a result set based on the rows based on one table
  4. It creates a result set by joining two tables together and taking all the rows in common plus the rows belonging to one of the tables

Answer: B. It creates a result set based on the rows in common between two tables

Explanation

The answer that comes closest to the truth is definitely Answer B. Inner joins are used to join the contents of two tables based on a specific set of commonalities and then create a dataset that only contains rows in common between them.

Step-by-step solution

  1. An inner join returns only the rows where the join column matches in both tables.
  2. It does not create a permanent table.
  3. So option B, a result set of the rows in common, is the answer.

Why the other options are wrong

  • A. It creates a result set, not a permanent third table.
  • C. That describes one table only, not a join.
  • D. That describes an outer join, which keeps rows that have no match.

Tip: Inner join gives matching rows only; outer joins also keep unmatched rows.

Practise all 14 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all PHP topics · All topics