SQL Commands
SQL Commands
SELECT
Syntax:
SELECT * FROM table_name;
Explanation:
Retrieves all data (columns) from the specified table.
2. FROM
Syntax:
SELECT column1 FROM table_name;
Explanation:
Specifies the table from which to retrieve the data.
3. WHERE
Syntax:
SELECT * FROM employees WHERE city = 'Mumbai';
Explanation:
Filters records based on a condition.
4. ORDER BY
Syntax:
SELECT * FROM employee ORDER BY salary DESC;
Explanation:
Sorts the result in ascending (ASC) or descending (DESC) order.
5. GROUP BY
Syntax:
SELECT dept, COUNT(*) FROM employee GROUP BY dept;
Explanation:
Groups rows that have the same values into summary rows.
6. HAVING
Syntax:
SELECT dept, COUNT(*) FROM employee GROUP BY dept HAVING
COUNT(*) > 5;
Explanation:
Applies a condition on grouped rows (used with GROUP BY).
7. JOIN
Syntax:
SELECT e.ename, d.dname
FROM employee e
JOIN department d ON e.dno = d.dno;
Explanation:
Combines rows from two or more tables based on a related column.
8. INSERT INTO
Syntax:
INSERT INTO Book (book_id, title, author, cost)
VALUES (101, 'DBMS', 'Ramakrishnan', 500);
Explanation:
Inserts new data into a table.
9. UPDATE
Syntax:
UPDATE employee SET salary = salary * 1.10;
Explanation:
Updates existing records in a table.
10. DELETE
Syntax:
DELETE FROM employee WHERE ename = 'John';
Explanation:
Deletes records from a table.
11. LIKE
Syntax:
SELECT * FROM employee WHERE ename LIKE 'A%';
Explanation:
Searches for a pattern (e.g., names starting with 'A').
12. IN
Syntax:
SELECT * FROM employee WHERE dept IN ('HR', 'IT');
Explanation:
Checks if a value matches any value in a list.
13. BETWEEN
Syntax:
SELECT * FROM employee WHERE salary BETWEEN 30000 AND
60000;
Explanation:
Selects values within a specified range.
14. AND / OR
Syntax:
SELECT * FROM employee WHERE city = 'Mumbai' AND salary >
40000;
Explanation:
Combines multiple conditions using logical operators.
15. DISTINCT
Syntax:
SELECT DISTINCT city FROM employee;
Explanation:
Returns only unique (non-duplicate) values.
16. COUNT()
Syntax:
SELECT COUNT(*) FROM employee;
Explanation:
Counts the number of rows in a table.
18. SUM()
Syntax:
SELECT SUM(salary) FROM employee;
Explanation:
Calculates the total sum of a numeric column.
19. AVG()
Syntax:
SELECT AVG(salary) FROM employee;
Explanation:
Calculates the average value of a numeric column.