W3. SQL Select Top Flashcards
Q: What does the SQL SELECT TOP clause do?
A: It specifies the number of records to return from a query, often to improve performance on large tables.
Q: Write an example query to select the first 3 records from the “Customers” table using SELECT TOP.
SELECT TOP 3 * FROM Customers;
Q: Which SQL clause does MySQL use instead of SELECT TOP?
LIMIT
Q: Write a MySQL query to select the first 3 records from the “Customers” table.
SELECT * FROM Customers LIMIT 3;
Q: What clause does Oracle use to limit records?
A: FETCH FIRST n ROWS ONLY or ROWNUM
Q: Write an Oracle query to select the first 3 records from the “Customers” table.
SELECT * FROM Customers FETCH FIRST 3 ROWS ONLY;
Q: How do you select a percentage of records using SELECT TOP?
A: Add PERCENT after the number, e.g., SELECT TOP 50 PERCENT * FROM Customers;
Q: Write a SQL Server query to select the first 50% of records from the “Customers” table.
SELECT TOP 50 PERCENT * FROM Customers;
Q: How can you limit records with a condition (e.g., customers from Germany) using SELECT TOP?
SELECT TOP 3 * FROM Customers WHERE Country = 'Germany';
Q: Write a MySQL query to select the first 3 customers from Germany.
SELECT * FROM Customers WHERE Country = 'Germany' LIMIT 3;
Q: How do you sort and limit the result set using ORDER BY with SELECT TOP?
A: Add ORDER BY after specifying TOP, e.g., SELECT TOP 3 * FROM Customers ORDER BY CustomerName DESC;
Q: Write a query to select the top 3 customers sorted in reverse alphabetical order by CustomerName (for MySQL).
SELECT * FROM Customers ORDER BY CustomerName DESC LIMIT 3;
Q: Write an Oracle query to fetch the top 3 customers sorted in reverse alphabetical order by CustomerName.
SELECT * FROM Customers ORDER BY CustomerName DESC FETCH FIRST 3 ROWS ONLY;