SQL Flashcards
Q: What SQL clause is used to filter rows based on a specified condition?
Example:
How do you select employees with a salary greater than 5000?
A: The WHERE clause is used to filter rows based on a specified condition.
Example:
SELECT *
FROM Employees
WHERE salary > 5000;
Q: Which SQL keyword is used to sort the result set in ascending or descending order?
Example:
How do you sort employees by their last names in ascending order?
A: The ORDER BY keyword is used to sort the result set in ascending or descending order.
Example:
SELECT *
FROM Employees
ORDER BY last_name ASC;
Q: How do you select all columns from a table named “Customers”?
Example:
Write a query to select all data from the Customers table.
SELECT *
FROM Customers;
Q: What SQL function is used to count the number of rows in a result set?
Example:
How do you count the number of employees in the Employees table?
A: The COUNT() function is used to count the number of rows in a result set.
Example:
SELECT COUNT(*)
FROM Employees;
Q: How do you retrieve unique values from a column named “City” in a table named “Employees”?
Example:
Write a query to get unique cities from the Employees table.
SELECT DISTINCT City
FROM Employees;
Q: What SQL clause is used to group rows that have the same values in specified columns?
Example:
How do you group employees by their job titles?
A: The GROUP BY clause is used to group rows that have the same values in specified columns.
Example:
SELECT job_title, COUNT(*)
FROM Employees
GROUP BY job_title;
Q: Which SQL keyword is used to remove duplicate records from a result set?
Example:
How do you select unique job titles from the Employees table?
A: The DISTINCT keyword is used to remove duplicate records from a result set.
Example:
SELECT DISTINCT job_title
FROM Employees;
Q: What SQL function is used to calculate the average value of a numeric column?
Example:
How do you find the average salary of all employees?
A: The AVG() function is used to calculate the average value of a numeric column.
Example:
SELECT AVG(salary)
FROM Employees;
Q: How do you rename a column in the result set using an alias?
Example:
How do you select the first name of employees and rename it to ‘FirstName’ in the result set?
A: Use the AS keyword to rename a column in the result set.
Example:
SELECT first_name AS FirstName
FROM Employees;
Q: What SQL keyword is used to combine rows from two or more tables, based on a related column between them?
Example:
How do you join Employees and Departments tables to get the department names for each employee?
A: The JOIN keyword is used to combine rows from two or more tables based on a related column between them.
Example:
SELECT Employees.first_name, Employees.last_name, Departments.department_name
FROM Employees
JOIN Departments ON Employees.department_id = Departments.department_id;