Hi! Could we please enable some services and cookies to improve your experience and our website?

SQLize | PHPize | SQLtest

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

A A A
Login    Share code      Blog   FAQ
Copy Format Clear
-- Step 1: Create Tables CREATE TABLE Students ( StudentID INT PRIMARY KEY, Name VARCHAR(100), Age INT ); CREATE TABLE Courses ( CourseID INT PRIMARY KEY, CourseName VARCHAR(100), Credits INT ); CREATE TABLE Enrollments ( EnrollmentID INT PRIMARY KEY, StudentID INT, CourseID INT, EnrollmentDate DATE, FOREIGN KEY (StudentID) REFERENCES Students(StudentID), FOREIGN KEY (CourseID) REFERENCES Courses(CourseID) ); -- Step 2: Insert Data INSERT INTO Students VALUES (1, 'Alice', 14); INSERT INTO Students VALUES (2, 'Bob', 15); INSERT INTO Courses VALUES (101, 'Math', 3); INSERT INTO Courses VALUES (102, 'Science', 4); INSERT INTO Enrollments VALUES (1, 1, 101, '2025-07-01'); INSERT INTO Enrollments VALUES (2, 2, 102, '2025-07-02'); -- Step 3: Query Data -- Show all enrollments with student and course names SELECT e.EnrollmentID, s.Name AS StudentName, c.CourseName, e.EnrollmentDate FROM Enrollments e JOIN Students s ON e.StudentID = s.StudentID JOIN Courses c ON e.CourseID = c.CourseID; -- Show how many students enrolled in each course SELECT c.CourseName, COUNT(*) AS TotalEnrolled FROM Enrollments e JOIN Courses c ON e.CourseID = c.CourseID GROUP BY c.CourseName;

Stuck with a problem? Got Error? Ask AI support!

Copy Clear