0% found this document useful (0 votes)
4 views3 pages

SQL Commands and Queries

The document outlines essential SQL commands and queries, including basic commands like SELECT, filtering with WHERE, and aggregate functions such as COUNT and AVG. It covers various types of joins, subqueries, and data manipulation commands for creating, altering, and deleting tables. Additionally, it introduces advanced concepts like window functions and common date functions.

Uploaded by

J Harish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

SQL Commands and Queries

The document outlines essential SQL commands and queries, including basic commands like SELECT, filtering with WHERE, and aggregate functions such as COUNT and AVG. It covers various types of joins, subqueries, and data manipulation commands for creating, altering, and deleting tables. Additionally, it introduces advanced concepts like window functions and common date functions.

Uploaded by

J Harish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

SQL Commands and Queries

1. Basic SQL Commands:

----------------------------

SELECT * FROM table_name;

SELECT column1, column2 FROM table_name;

WHERE, AND, OR

ORDER BY column ASC/DESC;

LIMIT N;

DISTINCT column;

2. Filtering and Conditions:

----------------------------

SELECT * FROM employees WHERE department = 'Sales' AND salary > 50000;

3. Aggregate Functions:

----------------------------

SELECT COUNT(*) FROM employees;

SELECT SUM(salary) FROM employees;

SELECT AVG(salary) FROM employees;

SELECT MIN(age) FROM employees;

SELECT MAX(age) FROM employees;

GROUP BY and HAVING:

SELECT department, COUNT(*) FROM employees GROUP BY department;


SELECT department, AVG(salary) FROM employees GROUP BY department HAVING AVG(salary)

> 60000;

4. Joins:

----------------------------

INNER JOIN:

SELECT * FROM employees e INNER JOIN departments d ON e.dept_id = d.dept_id;

LEFT JOIN:

SELECT * FROM employees e LEFT JOIN departments d ON e.dept_id = d.dept_id;

RIGHT JOIN:

SELECT * FROM employees e RIGHT JOIN departments d ON e.dept_id = d.dept_id;

FULL OUTER JOIN:

SELECT * FROM employees e FULL OUTER JOIN departments d ON e.dept_id = d.dept_id;

5. Subqueries:

----------------------------

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

6. Data Definition Language (DDL):

----------------------------

CREATE TABLE employees (id INT PRIMARY KEY, name VARCHAR(50));

ALTER TABLE employees ADD email VARCHAR(100);

DROP TABLE employees;


7. Data Manipulation Language (DML):

----------------------------

INSERT INTO employees (id, name, salary) VALUES (1, 'John', 50000);

UPDATE employees SET salary = 60000 WHERE id = 1;

DELETE FROM employees WHERE id = 1;

8. Window Functions (Advanced):

----------------------------

SELECT name, department, salary,

RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS salary_rank

FROM employees;

9. Common Date Functions:

----------------------------

SELECT CURRENT_DATE;

SELECT NOW();

SELECT EXTRACT(YEAR FROM date_column);

SELECT DATEDIFF(NOW(), hire_date) FROM employees;

You might also like