SQL Query Flashcards
What is querying in SQL?
The process of retrieving specific data from a database.
What is the typical syntax structure of an SQL query?
SELECT … FROM … WHERE … GROUP BY … HAVING … ORDER BY … LIMIT …
Which clause is executed last in SQL query execution?
The SELECT clause.
What is the purpose of the SELECT clause?
To specify which columns or expressions to retrieve.
What does the FROM clause do?
Specifies the table(s) from which to retrieve data.
What is the purpose of the WHERE clause?
To filter rows based on specified conditions.
What does the GROUP BY clause do?
Groups rows that have the same values in specified columns.
What does the HAVING clause do?
Filters groups created by GROUP BY based on conditions.
What is the role of ORDER BY?
To sort the query result set based on one or more columns.
What does the LIMIT clause do?
Restricts the number of rows returned by the query.
What does OFFSET do when used with LIMIT?
Specifies the starting point from which rows are returned.
Which clause is responsible for filtering rows before grouping?
WHERE clause.
Which clause filters grouped data?
HAVING clause.
Which clause determines which table data comes from?
FROM clause.
Which clause defines how many records are returned?
LIMIT clause.
If you want to find all employees in the ‘Sales’ department, what clause should you use?
WHERE clause.
If you want to group sales data by region, which clause is needed?
GROUP BY clause.
If you want to show only regions with total sales over $10,000, which clause is necessary?
HAVING clause.
To show the top 5 highest-paid employees, what clauses might you use?
ORDER BY salary DESC LIMIT 5.
What does the COUNT() function do?
Counts the number of rows in a specified column.
What does SUM() function do?
Calculates the total sum of a column.
What does AVG() function do?
Calculates the average value of a column.
What does MIN() function do?
Finds the minimum value in a column.
What does MAX() function do?
Finds the maximum value in a column.
What does DISTINCT do in a SELECT query?
Removes duplicate rows from the result set.
What is the purpose of AS in a SELECT query?
Gives an alias (temporary name) to a column or expression.
Spot the mistake: ‘SELECT name salary FROM employees;’
Missing comma. Correct syntax: SELECT name, salary FROM employees;
Spot the mistake: ‘SELECT * employees;’
Missing FROM clause. Correct syntax: SELECT * FROM employees;
Spot the mistake: ‘ORDER name BY ASC;’
Correct syntax: ORDER BY name ASC;
What is the correct order of clauses in an SQL SELECT query?
SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT.
Which clause is conceptually executed before SELECT but written after it?
FROM clause.
Does SQL execute SELECT clause first or last?
Last.
If you write WHERE before FROM, will the query work?
No, SQL requires FROM before WHERE.
What happens if you skip WHERE in a DELETE operation?
All rows will be affected.
If you use HAVING without GROUP BY, what does it apply to?
The entire result set, treated as a single group.
Explain in your own words the difference between WHERE and HAVING.
WHERE filters rows before grouping, HAVING filters groups after grouping.
Describe the purpose of ORDER BY in a query.
It sorts the results based on one or more columns in ascending or descending order.
What is the difference between COUNT() and SUM()?
COUNT() counts rows, SUM() totals values in a numeric column.