Jun 22, 2024
CREATE DATABASE database_name;
DROP DATABASE database_name;
USE database_name;
CREATE TABLE table_name (column_name data_type constraints);
CREATE TABLE Students (ID INT PRIMARY KEY, Name VARCHAR(50), Age INT NOT NULL);
CREATE TABLE Employees (EmployeeID INT PRIMARY KEY, Name VARCHAR(50), Salary FLOAT);
INSERT INTO table_name (columns) VALUES (values);
SELECT columns FROM table_name;
UPDATE table_name SET column=value WHERE condition;
DELETE FROM table_name WHERE condition;
GROUP BY
: Groups rows sharing a property so aggregate functions can be applied to each groupHAVING
: Filters groups based on conditionsCREATE VIEW view_name AS SELECT column(s) FROM table WHERE condition;
DROP VIEW view_name;
CREATE DATABASE College;
CREATE TABLE Students (ID INT PRIMARY KEY, Name VARCHAR(50), Age INT NOT NULL, City VARCHAR(50));
CREATE TABLE Courses (CourseID INT PRIMARY KEY, CourseName VARCHAR(50));
CREATE TABLE Enrollments (StudentID INT, CourseID INT,
FOREIGN KEY (StudentID) REFERENCES Students(ID),
FOREIGN KEY (CourseID) REFERENCES Courses(CourseID));
INSERT INTO Students VALUES (1, 'John Doe', 20, 'New York');
INSERT INTO Courses VALUES (101, 'Mathematics');
INSERT INTO Enrollments VALUES (1, 101);
SELECT Students.Name, Courses.CourseName FROM Students INNER JOIN Enrollments ON Students.ID = Enrollments.StudentID INNER JOIN Courses ON Enrollments.CourseID = Courses.CourseID;
SELECT Students.Name, Courses.CourseName FROM Students LEFT JOIN Enrollments ON Students.ID = Enrollments.StudentID LEFT JOIN Courses ON Enrollments.CourseID = Courses.CourseID;
SELECT Students.Name, Courses.CourseName FROM Students RIGHT JOIN Enrollments ON Students.ID = Enrollments.StudentID RIGHT JOIN Courses ON Enrollments.CourseID = Courses.CourseID;
SELECT Students.Name, Courses.CourseName FROM Students FULL OUTER JOIN Enrollments ON Students.ID = Enrollments.StudentID FULL OUTER JOIN Courses ON Enrollments.CourseID = Courses.CourseID;
CREATE VIEW StudentCourses AS SELECT Students.Name, Courses.CourseName FROM Students INNER JOIN Enrollments ON Students.ID = Enrollments.StudentID INNER JOIN Courses ON Enrollments.CourseID = Courses.CourseID;
SELECT * FROM StudentCourses;
DROP VIEW StudentCourses;