MySQL Quick Revision Notes
What is MySQL?
- MySQL is a popular open-source relational database management system (RDBMS).
Basic SQL Commands:
- CREATE TABLE: Create a new table
- INSERT INTO: Add data to a table
- SELECT: Retrieve data from a table
- UPDATE: Modify data
- DELETE: Remove data
Example:
CREATE TABLE students (id INT PRIMARY KEY, name VARCHAR(50));
INSERT INTO students VALUES (1, 'Hemant');
SELECT * FROM students;
UPDATE students SET name='Rahul' WHERE id=1;
DELETE FROM students WHERE id=1;
Clauses:
- WHERE: Filters rows (SELECT * FROM students WHERE id=1);
- ORDER BY: Sorts results (SELECT * FROM students ORDER BY name);
- GROUP BY: Groups rows (used with aggregate functions);
- HAVING: Filters groups (like WHERE but used with GROUP BY);
Joins:
- INNER JOIN: Returns matching rows from both tables
- LEFT JOIN: All rows from left table + matched rows from right
- RIGHT JOIN: All rows from right table + matched rows from left
Example:
SELECT s.name, m.marks FROM students s
INNER JOIN marks m ON s.id = m.student_id;
Constraints:
- PRIMARY KEY: Uniquely identifies each row
- FOREIGN KEY: Links two tables
- NOT NULL: Value cannot be empty
- UNIQUE: All values must be different
- DEFAULT: Sets default value
Aggregate Functions:
- COUNT(): Counts rows
- SUM(): Adds values
- AVG(): Average value
- MAX(): Highest value
- MIN(): Lowest value
Other Important Concepts:
- Normalization: Reducing data redundancy
- Index: Speeds up search operations
- Views: Virtual tables
- Transactions: Group of SQL operations (COMMIT, ROLLBACK)
- ACID Properties: Atomicity, Consistency, Isolation, Durability
Interview Questions:
1. What is MySQL?
- A relational database used to store structured data.
2. What is a primary key?
- A column that uniquely identifies each row.
3. Difference between WHERE and HAVING?
- WHERE filters rows; HAVING filters groups.
4. What is a JOIN? Types?
- Combines data from multiple tables: INNER, LEFT, RIGHT, FULL
5. What is normalization?
- Organizing data to reduce redundancy.
6. What is a transaction?
- A sequence of SQL operations treated as a single unit.