🐬 Ultimate Guide to MySQL – Complete SQL Cheat Sheet & Best Practices

🐬 Ultimate Guide to MySQL – Complete SQL Cheat Sheet & Best Practices

🌱 Introduction to MySQL

MySQL is an open-source Relational Database Management System (RDBMS) that uses Structured Query Language (SQL) to manage and manipulate data. It is widely used for web applications, analytics, and enterprise solutions.


πŸ“Œ What is a Database?

A database is a structured collection of data stored in a way that makes retrieval and management easy.

βœ… Types of Databases:

  • Relational (SQL-based) – Data is stored in tables with predefined relationships.

    • Examples: MySQL, PostgreSQL, Oracle.
  • Non-Relational (NoSQL) – Uses flexible data models like key-value, document, or graph-based storage.

    • Examples: MongoDB, Cassandra, Firebase.

πŸ”₯ Installing MySQL on Windows, Linux, and Ubuntu

🏁 Windows:

  1. Download MySQL from MySQL Official Website.

  2. Run the installer and select "Developer Default."

  3. Set a root password during installation.

  4. Verify installation by opening Command Prompt and running:

     mysql --version
    

🐧 Linux (Ubuntu/Debian):

  1. Update package lists:

     sudo apt update
    
  2. Install MySQL:

     sudo apt install mysql-server
    
  3. Secure MySQL installation:

     sudo mysql_secure_installation
    
  4. Verify installation:

     mysql --version
    

πŸ“Œ SQL vs MySQL – What’s the Difference?

FeatureSQLMySQL
DefinitionStructured Query LanguageDatabase management system using SQL
UsageLanguage for querying databasesSoftware that uses SQL for queries
ComplexityStandardizedImplementation varies
VendorUniversalOracle Corporation
ExampleSELECT * FROM users;MySQL supports SELECT * FROM users;

πŸ› οΈ Basic MySQL Commands

πŸ“Œ Database Commands

-- Create a new database
CREATE DATABASE mydatabase;

-- Use a specific database
USE mydatabase;

-- Show all databases
SHOW DATABASES;

πŸ“Œ Table Commands

-- Create a table
CREATE TABLE students (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100),
    age INT
);

-- Show all tables
SHOW TABLES;

-- Describe table structure
DESC students;

πŸ“Œ Insert Data

INSERT INTO students (name, age) VALUES ('Alice', 21), ('Bob', 22);

πŸ“Œ Select and Filter Data

-- Select all rows
SELECT * FROM students;

-- Select specific columns
SELECT name, age FROM students;

-- Use WHERE clause
SELECT * FROM students WHERE age > 20;

πŸ“Œ Updating Data

-- Update a record
UPDATE students SET age = 23 WHERE name = 'Alice';

πŸ“Œ Deleting Data

-- Delete a record
DELETE FROM students WHERE name = 'Bob';

πŸ“Œ Constraints

ConstraintDescription
PRIMARY KEYUniquely identifies each row
FOREIGN KEYReferences another table’s key
NOT NULLEnsures a column cannot be null
UNIQUEEnsures unique values
CHECKEnforces a condition

πŸ“Œ Using JOINS

-- INNER JOIN
SELECT employees.name, departments.department_name
FROM employees
INNER JOIN departments ON employees.department_id = departments.id;

-- LEFT JOIN
SELECT employees.name, departments.department_name
FROM employees
LEFT JOIN departments ON employees.department_id = departments.id;

πŸ“Š Aggregate Functions

-- Count the number of records
SELECT COUNT(*) FROM students;

-- Find the average age
SELECT AVG(age) FROM students;

-- Find the maximum age
SELECT MAX(age) FROM students;

πŸ“Œ Subqueries

-- Find students older than the average age
SELECT name FROM students WHERE age > (SELECT AVG(age) FROM students);

πŸ“Œ EXISTS and NOT EXISTS

-- EXISTS Example
SELECT name FROM students WHERE EXISTS (SELECT id FROM cities WHERE name = 'London');

-- NOT EXISTS Example
SELECT name FROM students WHERE NOT EXISTS (SELECT id FROM cities WHERE name = 'London');

πŸ”₯ Top 19 MySQL Interview Questions

  1. What is MySQL?

    • MySQL is a relational database management system (RDBMS) used for data storage and retrieval.
  2. What is the difference between SQL and MySQL?

    • SQL is a query language; MySQL is a database system that uses SQL.
  3. What are the different types of joins in SQL?

    • INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN.
  4. How do you create a foreign key in MySQL?

     CREATE TABLE orders (
         id INT PRIMARY KEY AUTO_INCREMENT,
         customer_id INT,
         FOREIGN KEY (customer_id) REFERENCES customers(id)
     );
    
  5. How do you use the LIKE operator?

     SELECT * FROM students WHERE name LIKE 'A%';
    
  6. What is an index in MySQL?

    • An index speeds up searches on large datasets.
  7. What is the difference between WHERE and HAVING?

    • WHERE filters rows before aggregation; HAVING filters aggregated results.
  8. What is ACID compliance?

    • Atomicity, Consistency, Isolation, and Durability ensure data reliability.
  9. What is a stored procedure?

     CREATE PROCEDURE GetStudents()
     BEGIN
         SELECT * FROM students;
     END;
    
  10. What is a trigger in MySQL?

    • A trigger automatically executes a function when a condition is met.

🎯 Conclusion

Mastering MySQL allows you to efficiently manage and query databases for applications, analytics, and enterprise solutions. By understanding its commands, joins, subqueries, and best practices, you can optimize data storage and retrieval efficiently.

πŸ’‘ Happy Learning! πŸš€

Β