Multiple choice technology databases

With SQL, how do you select all the records from a table named "Persons" where the "LastName" is alphabetically between (and including) "Hansen" and "Pettersen"?

  1. SELECT LastName>'Hansen' AND LastName<'Pettersen' FROM Persons

  2. SELECT * FROM Persons WHERE LastName>'Hansen' AND LastName<'Pettersen'

  3. SELECT * FROM Persons WHERE LastName BETWEEN 'Hansen' AND 'Pettersen'

  4. SELECT * FROM Persons WHERE LastName IN 'Hansen' AND 'Pettersen'

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

To solve this question, the user needs to know how to use SQL to select specific records from a table based on certain criteria. Specifically, they need to know how to use the WHERE clause to filter records based on a specified range of values.

Now, let's go through each option and explain why it is right or wrong:

A. SELECT LastName>'Hansen' AND LastName<'Pettersen' FROM Persons This option is incorrect because it uses an invalid syntax to filter the records based on the range of values. The AND operator used here is incorrect. When combining conditions, we use the OR operator. Therefore, this option is incorrect.

B. SELECT * FROM Persons WHERE LastName>'Hansen' AND LastName<'Pettersen' This option is correct. It uses the correct syntax to filter the records based on the range of values. The WHERE clause is used to filter the records with the specified LastName that is between the "Hansen" and "Pettersen" range.

C. SELECT * FROM Persons WHERE LastName BETWEEN 'Hansen' AND 'Pettersen' This option is correct. It uses the correct syntax to filter the records based on the range of values. The BETWEEN keyword is used to specify that we want all the records with LastName between 'Hansen' and 'Pettersen' range.

D. SELECT * FROM Persons WHERE LastName IN 'Hansen' AND 'Pettersen' This option is incorrect because the IN keyword is used to select records where the LastName matches any of the specified values in a list. In this case, we want to select the records that are between the range of values. Therefore, this option is incorrect.

The Answer is: C or B