W3. SQL In Flashcards
Q: What does the SQL IN operator do?
A: It allows you to specify multiple values in a WHERE clause, acting as a shorthand for multiple OR conditions.
Q: Write a query to select all customers from ‘Germany’, ‘France’, or ‘UK’.
SELECT * FROM Customers
WHERE Country IN (‘Germany’, ‘France’, ‘UK’);
Q: What is the syntax for using the IN operator in SQL?
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, …);
Q: How do you exclude multiple values using IN?
A: Use NOT IN before the list of values, e.g., WHERE Country NOT IN (‘Germany’, ‘France’, ‘UK’).
Q: Write a query to select all customers who are NOT from ‘Germany’, ‘France’, or ‘UK’.
SELECT * FROM Customers
WHERE Country NOT IN (‘Germany’, ‘France’, ‘UK’);
Q: Can IN be used with a subquery?
A: Yes, IN can be used to match records in the main query with those in the result of a subquery.
Q: Write a query to select all customers who have an order in the “Orders” table.
SELECT * FROM Customers
WHERE CustomerID IN (SELECT CustomerID FROM Orders);
Q: What does NOT IN do when used with a subquery?
A: It selects records from the main query that do not match any in the subquery result.
Q: Write a query to return all customers who have NOT placed any orders.
SELECT * FROM Customers
WHERE CustomerID NOT IN (SELECT CustomerID FROM Orders);