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 * FROM Persons WHERE LastName>'Hansen' AND LastName<'Pettersen'

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

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

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

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

To solve this question, the user needs to know the syntax for the SELECT statement in SQL and how to use the WHERE clause to filter records based on a specific condition. The user must also understand the BETWEEN operator in SQL that is used to select values within a range.

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

A. SELECT * FROM Persons WHERE LastName>'Hansen' AND LastName<'Pettersen' This option is incorrect. The query selects records whose last name is greater than "Hansen" and less than "Pettersen," but does not include the records with last name "Hansen" and "Pettersen." We need to include the "Hansen" and "Pettersen" records in the query.

B. SELECT * FROM Persons WHERE LastName BETWEEN 'Hansen' AND 'Pettersen' This option is correct. The query selects all records from the "Persons" table where the "LastName" is alphabetically between (and including) "Hansen" and "Pettersen". The BETWEEN operator is used to select values within a range.

C. SELECT LastName>'Hansen' AND LastName<'Pettersen' FROM Persons This option is incorrect. The syntax is incorrect. We need to use the SELECT statement to select columns, not conditions. Also, we need to use the WHERE clause to filter records, not the SELECT statement.

D. SELECT LastName<>'Hansen' AND LastName<>'Pettersen' FROM Persons This option is incorrect. The query selects all the records except the records with the last name "Hansen" and "Pettersen" from the "Persons" table, which is not what we want. We need to select records with last name between (and including) "Hansen" and "Pettersen".

The Answer is: B