[Link] is SQL?
SQL (Structured Query Language) is used to create, store, retrieve, and manage data
in relational databases.
Example: SELECT * FROM students;
2. What is DDL?
DDL (Data Definition Language) is used to define and modify database structures like
tables.
Commands: CREATE, DROP, ALTER, TRUNCATE.
3. CREATE command
Used to create a new table or database.
Example: CREATE TABLE students(id INT, name VARCHAR(50));
4. DROP command
Deletes the table completely along with its data.
Example: DROP TABLE students;
5. TRUNCATE command
Removes all records from a table but keeps the structure.
Example: TRUNCATE TABLE students;
6. ALTER command
Used to modify the structure of an existing table.
Example: ALTER TABLE students ADD age INT;
7. Add column using ALTER
Adds a new column to a table.
Example: ALTER TABLE students ADD email VARCHAR(100);
8. Modify column datatype
Changes the datatype of an existing column.
Example: ALTER TABLE students MODIFY name VARCHAR(100);
9. Rename column
Changes the name of a column.
Example: ALTER TABLE students RENAME COLUMN name TO full_name;
10. Difference between DROP and TRUNCATE
DROP removes table + structure; TRUNCATE removes only data.
DROP is irreversible, TRUNCATE is faster.
11. What is DML?
DML (Data Manipulation Language) is used to manage data in tables.
Commands: INSERT, UPDATE, DELETE.
12. INSERT command
Adds new records to a table.
Example: INSERT INTO students VALUES(1, 'Madhumitha');
13. Insert multiple records
Inserts many rows at once.
Example: INSERT INTO students VALUES (2,'Asha'), (3,'Ravi');
14. UPDATE command
Modifies existing records.
Example: UPDATE students SET name='Anu' WHERE id=1;
15. DELETE command
Removes specific records from a table.
Example: DELETE FROM students WHERE id=3;
16. Update salary by 10%
Example: UPDATE employees SET salary = salary + salary*0.10;
17. Delete specific rows
Example: DELETE FROM employees WHERE dept='HR';
18. Difference between DELETE and DROP
DELETE removes rows;
DROP removes the entire table.
DELETE can use WHERE;
DROP cannot.
19. Insert data from another table
Example: INSERT INTO new_table SELECT * FROM old_table;
20. What is DQL?
DQL (Data Query Language) is used to fetch data from tables.
Main command: SELECT.
21. SELECT command
Retrieves data from a table.
Example: SELECT * FROM students;
22. Select specific columns
Example: SELECT name, email FROM students;
23. DISTINCT keyword
Removes duplicate values from result.
Example: SELECT DISTINCT dept FROM employees;
24. WHERE clause
Filters records based on a condition.
Example: SELECT * FROM students WHERE id=1;
29. IS NULL
Checks for NULL values.
Example: SELECT * FROM students WHERE email IS NULL;
31. GROUP BY clause
Groups rows that have the same values in a column and applies aggregate functions.
Example: SELECT dept FROM employees GROUP BY dept;
32. GROUP BY with COUNT
Counts records in each group.
Example: SELECT dept, COUNT(*) FROM employees GROUP BY dept;
33. GROUP BY with SUM
Finds total of a column for each group.
Example: SELECT dept, SUM(salary) FROM employees GROUP BY dept;
34. HAVING clause
Filters grouped results (used with GROUP BY).
Example:SELECT dept, COUNT(*) FROM employees GROUP BY dept HAVING
COUNT(*) > 2;
35. Difference between WHERE and HAVING
WHERE filters rows before grouping;
HAVING filters groups after GROUP BY.
WHERE cannot use aggregate functions, HAVING can.
36. ORDER BY clause
Sorts the result set.
Example: SELECT * FROM employees ORDER BY salary;
37. Ascending order
Sorts from lowest to highest (default).
Example: SELECT * FROM employees ORDER BY salary ASC;
38. Descending order
Sorts from highest to lowest.
Example: SELECT * FROM employees ORDER BY salary DESC;
39. LIMIT clause
Restricts number of rows returned.
Example: SELECT * FROM employees LIMIT 3;
40. Fetch top 5 records
Example: SELECT * FROM employees ORDER BY salary DESC LIMIT 5;
41. Constraints
Rules applied on columns to ensure data integrity.
Examples: NOT NULL, UNIQUE, CHECK, DEFAULT.
42. NOT NULL constraint
Prevents NULL values.
Example: name VARCHAR(50) NOT NULL
43. UNIQUE constraint
Ensures all values are different.
Example: email VARCHAR(100) UNIQUE
46. DEFAULT constraint
Sets a default value.
Example: status VARCHAR(10) DEFAULT 'Active'
47. CHECK constraint
Restricts values based on condition.
Example: age INT CHECK(age >= 18)
48. Auto Increment
Automatically generates unique numbers.
Example: id INT AUTO_INCREMENT
49. Create table with constraints
Example: CREATE TABLE employees(
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE,
salary INT CHECK(salary > 0),
status VARCHAR(10) DEFAULT 'Active'
);
50. Increase salary by 5%
Example: UPDATE employees SET salary = salary + salary*0.05;
51. Second highest salary
Example: SELECT MAX(salary) FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
52. Third highest salary
Example: SELECT MAX(salary) FROM employees
WHERE salary < (
SELECT MAX(salary) FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees)
);
53. Count employees department wise
Example: SELECT dept, COUNT(*) FROM employees GROUP BY dept;
54. Max salary from table
Example: SELECT MAX(salary) FROM employees;
55. Min salary from table
Example: SELECT MIN(salary) FROM employees;
56. Average salary
Example: SELECT AVG(salary) FROM employees;
57. Total salary
Example: SELECT SUM(salary) FROM employees;
58. Employees joined after 2022
Example: SELECT * FROM employees WHERE join_date > '2022-12-31';