W3. SQL And Flashcards
Q: What does the SQL AND operator do?
A: It filters records based on multiple conditions, returning records only if all conditions are true.
Q: Write a SQL query to select all customers from Spain with names that start with “G.”
SELECT * FROM Customers WHERE Country = 'Spain' AND CustomerName LIKE 'G%';
Q: What is the syntax for using multiple AND conditions in SQL?
SELECT column1, column2, …
FROM table_name
WHERE condition1 AND condition2 AND condition3 …;
Q: How does the AND operator differ from the OR operator?
A: AND requires all conditions to be true, while OR requires only one condition to be true.
Q: Write a query to select all customers from Germany, in Berlin, with a PostalCode higher than 12000.
SELECT * FROM Customers WHERE Country = 'Germany' AND City = 'Berlin' AND PostalCode > 12000;
Q: Can you combine AND and OR operators in the same SQL query?
A: Yes, they can be combined, often with parentheses to ensure the correct order of conditions.
Q: Why are parentheses important when using AND and OR together?
A: They clarify grouping, ensuring that conditions are evaluated in the intended order.
Q: Write a query to select all Spanish customers with names starting with “G” or “R.”
SELECT * FROM Customers WHERE Country = 'Spain' AND (CustomerName LIKE 'G%' OR CustomerName LIKE 'R%');
Q: What happens if you omit parentheses when combining AND and OR?
A: SQL may misinterpret the intended condition grouping, potentially returning incorrect results.
Q: Write a query that returns customers who are either from Spain and start with “G” or start with “R” regardless of their country.
SELECT * FROM Customers WHERE Country = 'Spain' AND CustomerName LIKE 'G%' OR CustomerName LIKE 'R%';