```html SQL Basics for Beginners | Roshni Code Charm

🗄️ SQL Basics for Beginners 💻

Learn SQL from the beginning with simple examples. 🌱

📚 Table of Contents

  1. 🗄️ What is SQL?
  2. 💾 Create Database
  3. 📋 Create Table
  4. ➕ INSERT
  5. 🔍 SELECT
  6. 🎯 WHERE
  7. ↕️ ORDER BY
  8. 🔎 LIKE
  9. ✏️ UPDATE
  10. 🗑️ DELETE
  11. 🧮 SQL Functions
  12. 📊 GROUP BY
  13. 🔐 Constraints
  14. 🔗 JOINs
  15. 🛠️ ALTER TABLE
  16. 🎓 Practice Project
  17. 📌 Cheat Sheet

1. 🗄️ What is SQL?

SQL stands for Structured Query Language.

SQL is used to create, manage and retrieve data from databases.

💡 Tip: SQL is commonly used with MySQL, PostgreSQL, Oracle and SQL Server.

2. 💾 Create Database

Use the following command to create a database:

CREATE DATABASE college;

Select the database:

USE college;

3. 📋 Create Table

A table stores data in rows and columns.

CREATE TABLE students (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    age INT,
    course VARCHAR(50),
    marks INT
);

4. ➕ INSERT Data

INSERT INTO is used to add data to a table.

INSERT INTO students
(id, name, age, course, marks)
VALUES
(1, 'Riya', 19, 'BCA', 85);

Insert multiple records:

INSERT INTO students
(id, name, age, course, marks)
VALUES
(2, 'Aman', 20, 'BCA', 78),
(3, 'Neha', 19, 'BCA', 92),
(4, 'Rahul', 21, 'BCA', 67),
(5, 'Priya', 20, 'BCA', 88);

5. 🔍 SELECT Data

Display all records:

SELECT * FROM students;

Select specific columns:

SELECT name, marks
FROM students;

6. 🎯 WHERE Clause

WHERE is used to filter records.

SELECT *
FROM students
WHERE marks > 80;

Using multiple conditions:

SELECT *
FROM students
WHERE age = 20 AND marks > 80;

7. ↕️ ORDER BY

Sort marks from lowest to highest:

SELECT *
FROM students
ORDER BY marks ASC;

Sort marks from highest to lowest:

SELECT *
FROM students
ORDER BY marks DESC;

8. 🔎 LIKE

LIKE is used for searching a pattern.

SELECT *
FROM students
WHERE name LIKE 'R%';

This finds names starting with the letter R.

9. ✏️ UPDATE

UPDATE is used to change existing data.

UPDATE students
SET marks = 90
WHERE id = 2;
⚠️ Warning: Always check the WHERE condition before updating data.

10. 🗑️ DELETE

DELETE removes records from a table.

DELETE FROM students
WHERE id = 4;
⚠️ Without WHERE, DELETE can remove all records.

11. 🧮 SQL Functions

🔢 COUNT()

SELECT COUNT(*)
FROM students;

⬆️ MAX()

SELECT MAX(marks)
FROM students;

⬇️ MIN()

SELECT MIN(marks)
FROM students;

📈 AVG()

SELECT AVG(marks)
FROM students;

➕ SUM()

SELECT SUM(marks)
FROM students;

12. 📊 GROUP BY

GROUP BY groups rows having the same values.

SELECT age, COUNT(*)
FROM students
GROUP BY age;

🎯 HAVING

SELECT age, AVG(marks)
FROM students
GROUP BY age
HAVING AVG(marks) > 80;

13. 🔐 SQL Constraints

Constraint Purpose
🔑 PRIMARY KEY Uniquely identifies a record
🚫 NOT NULL Does not allow empty values
✨ UNIQUE Prevents duplicate values
⚙️ DEFAULT Provides a default value
🔗 FOREIGN KEY Connects two tables

14. 🔗 SQL JOINs

JOINs are used to combine data from multiple tables.

🔗 INNER JOIN

SELECT students.name,
       courses.course_name
FROM students
INNER JOIN courses
ON students.course_id = courses.course_id;

↔️ LEFT JOIN

SELECT students.name,
       courses.course_name
FROM students
LEFT JOIN courses
ON students.course_id = courses.course_id;

15. 🛠️ ALTER TABLE

➕ Add a new column:

ALTER TABLE students
ADD email VARCHAR(100);

✏️ Modify a column:

ALTER TABLE students
MODIFY name VARCHAR(100);

🗑️ Delete a column:

ALTER TABLE students
DROP COLUMN email;

16. 🎓 SQL Practice Project

🚀 Try creating a simple student management database.

CREATE DATABASE student_management;

USE student_management;

CREATE TABLE students (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    age INT,
    course VARCHAR(50),
    marks INT
);

INSERT INTO students
(id, name, age, course, marks)
VALUES
(1, 'Riya', 19, 'BCA', 85),
(2, 'Aman', 20, 'BCA', 78),
(3, 'Neha', 19, 'BCA', 92),
(4, 'Rahul', 21, 'BCA', 67),
(5, 'Priya', 20, 'BCA', 88);

SELECT * FROM students;

SELECT name, marks
FROM students
WHERE marks > 80;

SELECT *
FROM students
ORDER BY marks DESC;

SELECT MAX(marks)
FROM students;

SELECT AVG(marks)
FROM students;

SELECT COUNT(*)
FROM students;
💡 Practice Tip: Try every query yourself instead of only reading it.

17. 📌 SQL Cheat Sheet

Command Use
🗄️ CREATE DATABASE Create database
📋 CREATE TABLE Create table
➕ INSERT INTO Add data
🔍 SELECT Read data
🎯 WHERE Filter data
✏️ UPDATE Change data
🗑️ DELETE Delete data
↕️ ORDER BY Sort data
📊 GROUP BY Group data
🔗 JOIN Combine tables
🛠️ ALTER TABLE Change table structure