W3. SQL Between Flashcards
Q: What does the SQL BETWEEN operator do?
A: It selects values within a specified range, including the start and end values.
Q: Write a query to select all products with a price between 10 and 20.
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
Q: What is the syntax for the BETWEEN operator in SQL?
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Q: How do you select values outside a range using BETWEEN?
A: Use NOT BETWEEN, e.g., WHERE Price NOT BETWEEN 10 AND 20.
Q: Write a query to select all products with a price outside the range 10 to 20.
SELECT * FROM Products
WHERE Price NOT BETWEEN 10 AND 20;
Q: Can BETWEEN be combined with IN in SQL?
A: Yes, you can add IN to further filter values within the specified range.
Q: Write a query to select products with a price between 10 and 20 and a CategoryID of 1, 2, or 3.
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20
AND CategoryID IN (1,2,3);
Q: How can BETWEEN be used with text values?
A: It can select records in alphabetical order within a specified range of text values.
Q: Write a query to select products with ProductName alphabetically between “Carnarvon Tigers” and “Mozzarella di Giovanni”.
SELECT * FROM Products
WHERE ProductName BETWEEN ‘Carnarvon Tigers’ AND ‘Mozzarella di Giovanni’
ORDER BY ProductName;
Q: How do you exclude text values within a range using BETWEEN?
A: Use NOT BETWEEN with text values, e.g., WHERE ProductName NOT BETWEEN ‘Carnarvon Tigers’ AND ‘Mozzarella di Giovanni’.
Q: Write a query to select products with ProductName not between “Carnarvon Tigers” and “Mozzarella di Giovanni”.
SELECT * FROM Products
WHERE ProductName NOT BETWEEN ‘Carnarvon Tigers’ AND ‘Mozzarella di Giovanni’
ORDER BY ProductName;
Q: Can BETWEEN be used with dates?
A: Yes, it can select dates within a specified range, including start and end dates.
Q: Write a query to select all orders with OrderDate between ‘1996-07-01’ and ‘1996-07-31’.
SELECT * FROM Orders
WHERE OrderDate BETWEEN ‘1996-07-01’ AND ‘1996-07-31’;