```html
Learn SQL from the beginning with simple examples. 🌱
SQL stands for Structured Query Language.
SQL is used to create, manage and retrieve data from databases.
Use the following command to create a database:
CREATE DATABASE college;
Select the database:
USE college;
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
);
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);
Display all records:
SELECT * FROM students;
Select specific columns:
SELECT name, marks
FROM students;
WHERE is used to filter records.
SELECT *
FROM students
WHERE marks > 80;
Using multiple conditions:
SELECT *
FROM students
WHERE age = 20 AND marks > 80;
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;
LIKE is used for searching a pattern.
SELECT *
FROM students
WHERE name LIKE 'R%';
This finds names starting with the letter R.
UPDATE is used to change existing data.
UPDATE students
SET marks = 90
WHERE id = 2;
DELETE removes records from a table.
DELETE FROM students
WHERE id = 4;
SELECT COUNT(*)
FROM students;
SELECT MAX(marks)
FROM students;
SELECT MIN(marks)
FROM students;
SELECT AVG(marks)
FROM students;
SELECT SUM(marks)
FROM students;
GROUP BY groups rows having the same values.
SELECT age, COUNT(*)
FROM students
GROUP BY age;
SELECT age, AVG(marks)
FROM students
GROUP BY age
HAVING AVG(marks) > 80;
| 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 |
JOINs are used to combine data from multiple tables.
SELECT students.name,
courses.course_name
FROM students
INNER JOIN courses
ON students.course_id = courses.course_id;
SELECT students.name,
courses.course_name
FROM students
LEFT JOIN courses
ON students.course_id = courses.course_id;
➕ 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;
🚀 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;
| 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 |