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

Essential SQL Commands Guide

The document outlines essential SQL commands for database management, including creating and deleting databases and tables, as well as data manipulation commands such as insert, update, delete, and select. It also covers filtering data, aggregate functions, grouping and sorting, joins, subqueries, constraints, views, and other useful commands. This serves as a comprehensive guide for performing various operations in SQL databases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views3 pages

Essential SQL Commands Guide

The document outlines essential SQL commands for database management, including creating and deleting databases and tables, as well as data manipulation commands such as insert, update, delete, and select. It also covers filtering data, aggregate functions, grouping and sorting, joins, subqueries, constraints, views, and other useful commands. This serves as a comprehensive guide for performing various operations in SQL databases.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

🧱 1.

Database Commands
👉 Create / Delete Database

CREATE DATABASE database_name;


DROP DATABASE database_name;
USE database_name;

📋 2. Table Commands
👉 Create / Alter / Drop Table
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
...
);

ALTER TABLE table_name ADD column_name datatype;


ALTER TABLE table_name DROP COLUMN column_name;
ALTER TABLE table_name MODIFY column_name datatype;

DROP TABLE table_name;


TRUNCATE TABLE table_name;

📥 3. Data Manipulation (DML)


👉 Insert, Update, Delete, Select
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);

UPDATE table_name
SET column1 = value1, column2 = value2
WHERE condition;

DELETE FROM table_name


WHERE condition;

SELECT column1, column2


FROM table_name
WHERE condition;

🔍 4. Filtering Data
👉 WHERE, AND, OR, NOT, BETWEEN, IN, LIKE
SELECT * FROM employees WHERE salary > 50000;
SELECT * FROM employees WHERE city = 'Delhi' AND department = 'IT';
SELECT * FROM employees WHERE salary BETWEEN 30000 AND 60000;
SELECT * FROM employees WHERE department IN ('IT', 'HR');
SELECT * FROM employees WHERE name LIKE 'A%'; -- starts with A

🧮 5. Aggregate Functions
👉 COUNT, SUM, AVG, MIN, MAX
SELECT COUNT(*) FROM employees;
SELECT SUM(salary) FROM employees;
SELECT AVG(salary) FROM employees;
SELECT MIN(salary), MAX(salary) FROM employees;

📊 6. Grouping and Sorting


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

SELECT department, AVG(salary)


FROM employees
GROUP BY department
HAVING AVG(salary) > 40000;

SELECT * FROM employees ORDER BY salary DESC;

🔗 7. Joins
👉 Combine data from multiple tables
SELECT [Link], d.department_name
FROM employees e
INNER JOIN departments d ON e.department_id = d.department_id;

SELECT * FROM employees e


LEFT JOIN departments d ON e.department_id = d.department_id;

SELECT * FROM employees e


RIGHT JOIN departments d ON e.department_id = d.department_id;

SELECT * FROM employees e


FULL JOIN departments d ON e.department_id = d.department_id;

📤 8. Subqueries
SELECT name
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);

🧱 9. Constraints
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(50) UNIQUE,
salary DECIMAL(10,2) CHECK (salary > 0),
department_id INT FOREIGN KEY REFERENCES departments(id)
);

🔐 10. Views
CREATE VIEW high_salary_employees AS
SELECT name, salary FROM employees WHERE salary > 50000;

SELECT * FROM high_salary_employees;

DROP VIEW high_salary_employees;

⚙️11. Other Useful Commands


-- Rename a table
RENAME TABLE old_name TO new_name;

-- Copy table structure only


CREATE TABLE new_table AS SELECT * FROM old_table WHERE 1=0;

-- Copy table with data


CREATE TABLE new_table AS SELECT * FROM old_table;

You might also like