SQL QUERY
SQL QUERY
TRUNCATE Example:
sql
Copy code
This command will remove all rows from the employees table but leave the table structure intact.
DROP Example:
sql
Copy code
This command will completely remove the employees table from the database, including all its data
and structure.
• Example:
sql
Copy code
• Explanation: This query retrieves all records and columns from the employees table.
2. WHERE Clause
• Example:
sql
Copy code
• Explanation: This query retrieves all employees who work in the Sales department.
3. ORDER BY
• Example:
sql
Copy code
SELECT * FROM employees ORDER BY salary DESC;
• Explanation: This query retrieves all employees and sorts them by salary in descending
order.
• Example:
sql
Copy code
FROM employees
GROUP BY department;
• Explanation: This query groups employees by department and counts how many employees
are in each department.
5. HAVING Clause
• Example:
sql
Copy code
FROM employees
GROUP BY department
6. JOIN
• Example:
sql
Copy code
FROM employees e
ON e.department_id = d.id;
• Explanation: This query retrieves employee names along with their corresponding
department names by joining the employees and departments tables.
7. LEFT JOIN
• Example:
sql
Copy code
FROM employees e
ON e.department_id = d.id;
• Explanation: This query retrieves all employees, including those who do not have a
corresponding department (where department_id is NULL).
8. DISTINCT
• Example:
sql
Copy code
• Explanation: This query retrieves distinct department names from the employees table.
9. UNION
• Example:
sql
Copy code
UNION
• Explanation: This query combines the names of employees from the Sales and HR
departments.
10. SUBQUERY
• Query: Find employees who earn more than the average salary.
• Example:
sql
Copy code
SELECT name
FROM employees
• Explanation: This query retrieves the names of employees who earn more than the average
salary.
11. IN Clause
• Example:
sql
Copy code
SELECT *
FROM employees
• Explanation: This query retrieves employees who work in the Sales, HR, or IT departments.
12. EXISTS
• Example:
sql
Copy code
SELECT name
FROM employees e
WHERE EXISTS (
SELECT 1
FROM departments d
);
• Explanation: This query retrieves the names of employees who work in the Sales
department using the EXISTS condition.
13. UPDATE
• Query: Update the salary of employees in a specific department.
• Example:
sql
Copy code
UPDATE employees
• Explanation: This query increases the salary of all HR department employees by 10%.
14. DELETE
• Example:
sql
Copy code
• Explanation: This query deletes records of employees who have left the company.
• Example:
sql
Copy code
name VARCHAR(100),
department VARCHAR(50),
salary DECIMAL(10, 2)
);
• Explanation: This query creates a new table called employees with columns for id, name,
department, and salary.
sql
Copy code
• Explanation: This query adds a new hire_date column to the employees table.
17. INDEX
• Example:
sql
Copy code
• Explanation: This query creates an index on the department column of the employees table
to speed up searches.
18. BETWEEN
• Example:
sql
Copy code
SELECT *
FROM employees
• Explanation: This query retrieves employees whose salaries are between $50,000 and
$70,000.
• Example:
sql
Copy code
SELECT name,
salary,
CASE
WHEN salary > 70000 THEN 'High'
ELSE 'Low'
END AS salary_category
FROM employees;
• Explanation: This query categorizes employees' salaries into 'High', 'Medium', or 'Low'
categories.
20. TRUNCATE
• Query: Remove all data from a table without deleting the table itself.
• Example:
sql
Copy code
• Explanation: This query deletes all records from the employees table but keeps the table
structure.
These queries are commonly asked in SQL interviews to assess your ability to manipulate and
retrieve data from a database effectively. Make sure to practice them and understand their
applications.