Advance SQL Statements Flashcards
What is the SELECT TOP clause used for?
The SELECT TOP clause is used to specify the number of records to return.
The SELECT TOP clause can be very useful on large tables with thousands of records. Returning a large number of records can impact on performance.
Note: Not all database systems support the SELECT TOP clause.
What is the syntax for the SELECT TOP clause SQL Server / MS Access statement?
SELECT TOP number|percent column_name(s)
FROM table_name;
What is the syntax for the SELECT TOP clause MySQL statement?
SELECT column_name(s)
FROM table_name
LIMIT number;
Ex.
SELECT *
FROM Persons
LIMIT 5;
What is the syntax for the SELECT TOP clause Oracle statement?
SELECT column_name(s)
FROM table_name
WHERE ROWNUM <=5;
What is the SELECT TOP clause statement to select the first top 2 records?
SELECT TOP 2 * FROM Customers;
What is the SELECT TOP clause statement to select percent?
SELECT TOP 50 PERCENT * FROM Customers;
What is the LIKE operator used for?
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
What is the syntax for the LIKE operator statement?
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;
Ex.
SELECT * FROM Customers
WHERE City LIKE ‘s%’;
What is the statement for the LIKE operator when letters are missing?
SELECT * FROM Customers
WHERE City LIKE ‘%s’;
This statement selects all customers with a City ending with the letter “s”:
SELECT * FROM Customers
WHERE Country LIKE ‘%land%’;
This statement selects all customers with a Country containing the pattern “land”:
What is the statement for the LIKE operator using the NOT keyword?
Ex.
SELECT * FROM Customers
WHERE Country NOT LIKE ‘%land%’;
Using the NOT keyword allows you to select records that does NOT match the pattern.
This statement selects all customers with a Country NOT containing the pattern “land”:
What is the purpose of the IN operator?
The IN operator allows you to specify multiple values in a WHERE clause.
What is the syntax for the IN operator statement?
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,…);
Ex.
SELECT * FROM Customers
WHERE City IN (‘Paris’,’London’);
This statement selects all customers with a City of “Paris” or “London”:
What is the BETWEEN operator used for?
The BETWEEN operator is used to select values within a range.
What is the syntax for the BETWEEN operator statement?
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Ex.
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
This statement selects all products with a price BETWEEN 10 and 20:
What is the statement for the NOT BETWEEN operator?
SELECT * FROM Products
WHERE Price NOT BETWEEN 10 AND 20;
This statement displays the products outside the range of the previous example, use NOT BETWEEN:
What is the statement for the BETWEEN operator with IN?
SELECT * FROM Products
WHERE (Price BETWEEN 10 AND 20)
AND NOT CategoryID IN (1,2,3);
This statement selects all products with a price BETWEEN 10 and 20, but products with a CategoryID of 1,2, or 3 should not be displayed:
What is the statement for the BETWEEN operator with text value?
SELECT * FROM Products
WHERE ProductName BETWEEN ‘C’ AND ‘M’;
This statement selects all products with a ProductName beginning with any of the letter BETWEEN ‘C’ and ‘M’:
What is the statement for the NOT BETWEEN operator with text value?
SELECT * FROM Products
WHERE ProductName NOT BETWEEN ‘C’ AND ‘M’;
This statement selects all products with a ProductName beginning with any of the letter NOT BETWEEN ‘C’ and ‘M’:
What is the statement for the BETWEEN operator with date value?
SELECT * FROM Orders
WHERE OrderDate BETWEEN #07/04/1996# AND #07/09/1996#;
This statement selects all orders with an OrderDate BETWEEN ‘04-July-1996’ and ‘09-July-1996’:
What are JOIN used for?
An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them.