0% found this document useful (0 votes)
2 views

SQL QUERY

The document provides various SQL queries and their explanations, including commands for truncating and dropping tables, selecting data, filtering with WHERE, sorting with ORDER BY, grouping with GROUP BY, and joining tables. It also covers advanced topics like subqueries, the EXISTS clause, updating and deleting records, creating and altering tables, indexing, and using the CASE statement. These queries are essential for data manipulation and retrieval in SQL, often featured in interviews.

Uploaded by

hj05102006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL QUERY

The document provides various SQL queries and their explanations, including commands for truncating and dropping tables, selecting data, filtering with WHERE, sorting with ORDER BY, grouping with GROUP BY, and joining tables. It also covers advanced topics like subqueries, the EXISTS clause, updating and deleting records, creating and altering tables, indexing, and using the CASE statement. These queries are essential for data manipulation and retrieval in SQL, often featured in interviews.

Uploaded by

hj05102006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

SQL QUERY

TRUNCATE Example:

sql

Copy code

TRUNCATE TABLE employees;

This command will remove all rows from the employees table but leave the table structure intact.

DROP Example:

sql

Copy code

DROP TABLE employees;

This command will completely remove the employees table from the database, including all its data
and structure.

1. Basic SELECT Query

• Query: Retrieve all columns from a table.

• Example:

sql

Copy code

SELECT * FROM employees;

• Explanation: This query retrieves all records and columns from the employees table.

2. WHERE Clause

• Query: Retrieve records that meet a specific condition.

• Example:

sql

Copy code

SELECT * FROM employees WHERE department = 'Sales';

• Explanation: This query retrieves all employees who work in the Sales department.

3. ORDER BY

• Query: Retrieve data in a specific order.

• 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.

4. GROUP BY with Aggregate Functions

• Query: Count the number of employees in each department.

• Example:

sql

Copy code

SELECT department, COUNT(*) AS num_employees

FROM employees

GROUP BY department;

• Explanation: This query groups employees by department and counts how many employees
are in each department.

5. HAVING Clause

• Query: Use HAVING to filter results after grouping.

• Example:

sql

Copy code

SELECT department, COUNT(*) AS num_employees

FROM employees

GROUP BY department

HAVING COUNT(*) > 10;

• Explanation: This query returns departments with more than 10 employees.

6. JOIN

• Query: Combine data from two tables based on a related column.

• Example:

sql

Copy code

SELECT e.name, d.department_name

FROM employees e

INNER JOIN departments d

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

• Query: Retrieve all employees, even those without a matching department.

• Example:

sql

Copy code

SELECT e.name, d.department_name

FROM employees e

LEFT JOIN departments d

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

• Query: Retrieve unique department names.

• Example:

sql

Copy code

SELECT DISTINCT department FROM employees;

• Explanation: This query retrieves distinct department names from the employees table.

9. UNION

• Query: Combine results from two different queries.

• Example:

sql

Copy code

SELECT name FROM employees WHERE department = 'Sales'

UNION

SELECT name FROM employees WHERE department = 'HR';

• 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

WHERE salary > (SELECT AVG(salary) FROM employees);

• Explanation: This query retrieves the names of employees who earn more than the average
salary.

11. IN Clause

• Query: Find employees who work in specific departments.

• Example:

sql

Copy code

SELECT *

FROM employees

WHERE department IN ('Sales', 'HR', 'IT');

• Explanation: This query retrieves employees who work in the Sales, HR, or IT departments.

12. EXISTS

• Query: Check if any employee belongs to a specific department.

• Example:

sql

Copy code

SELECT name

FROM employees e

WHERE EXISTS (

SELECT 1

FROM departments d

WHERE e.department_id = d.id AND d.department_name = 'Sales'

);

• 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

SET salary = salary * 1.1

WHERE department = 'HR';

• Explanation: This query increases the salary of all HR department employees by 10%.

14. DELETE

• Query: Delete employees who have left the company.

• Example:

sql

Copy code

DELETE FROM employees

WHERE status = 'Left';

• Explanation: This query deletes records of employees who have left the company.

15. CREATE TABLE

• Query: Create a new table to store employee data.

• Example:

sql

Copy code

CREATE TABLE employees (

id INT PRIMARY KEY,

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.

16. ALTER TABLE

• Query: Add a new column to an existing table.


• Example:

sql

Copy code

ALTER TABLE employees

ADD COLUMN hire_date DATE;

• Explanation: This query adds a new hire_date column to the employees table.

17. INDEX

• Query: Create an index on a table to improve query performance.

• Example:

sql

Copy code

CREATE INDEX idx_department ON employees(department);

• Explanation: This query creates an index on the department column of the employees table
to speed up searches.

18. BETWEEN

• Query: Find employees with salaries in a specific range.

• Example:

sql

Copy code

SELECT *

FROM employees

WHERE salary BETWEEN 50000 AND 70000;

• Explanation: This query retrieves employees whose salaries are between $50,000 and
$70,000.

19. CASE Statement

• Query: Categorize employees based on their salary.

• Example:

sql

Copy code

SELECT name,

salary,

CASE
WHEN salary > 70000 THEN 'High'

WHEN salary BETWEEN 50000 AND 70000 THEN 'Medium'

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

TRUNCATE TABLE employees;

• 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.

You might also like