7.8k views
2 votes
Which SQL query selects all students with age 19 and address 'Newtown,' or age 20 and address 'Bondi'?

Option 1:
sql
Copy code
SELECT * FROM Student WHERE age = 19 AND address = 'Newtown' OR age = 20 AND address = 'Bondi';
Option 2:
sql
Copy code
SELECT * FROM Student WHERE (age = 19 AND address = 'Newtown') OR (age = 20 AND address = 'Bondi');
Option 3:
sql
Copy code
SELECT * FROM Student WHERE (age = 19 OR age = 20) AND (address = 'Newtown' OR address = 'Bondi');
Option 4:
sql
Copy code
SELECT * FROM Student WHERE age IN (19, 20) AND address IN ('Newtown', 'Bondi');

1 Answer

4 votes

Final answer:

The correct SQL query to select students with the specified age and address conditions is Option 2, which uses parentheses to group the conditions properly.

Step-by-step explanation:

The correct SQL query to select all students with age 19 and address 'Newtown,' or age 20 and address 'Bondi' is Option 2. This is because logical operators in SQL (AND, OR) must be used carefully to ensure the correct logical evaluation order. Without parentheses, AND binds more tightly than OR, which can lead to unexpected results. Therefore, it is essential to use parentheses to group the conditions properly.

Option 2 is:

SELECT * FROM Student WHERE (age = 19 AND address = 'Newtown') OR (age = 20 AND address = 'Bondi');

This query makes sure that the students selected are either 19 years old living in Newtown or 20 years old living in Bondi, which matches the requested criteria.

User Jonathan Michalik
by
7.8k points