Learn Basic SQL commands

To practice Basic SQL Commands we will start from Basic. First, we will create a college database that we can use to try out the basic SQL commands.

CREATE DATABASE college;

USE college;

CREATE TABLE students (
  id INT PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  major VARCHAR(255) NOT NULL,
  year INT NOT NULL
);

CREATE TABLE courses (
  id INT PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  credits INT NOT NULL
);

CREATE TABLE enrollment (
  student_id INT NOT NULL,
  course_id INT NOT NULL,
  grade CHAR(1),
  PRIMARY KEY (student_id, course_id),
  FOREIGN KEY (student_id) REFERENCES students(id),
  FOREIGN KEY (course_id) REFERENCES courses(id)
);
INSERT INTO students (id, name, major, year)
VALUES (1, 'Alice', 'Computer Science', 2021),
       (2, 'Bob', 'Electrical Engineering', 2020),
       (3, 'Charlie', 'Mechanical Engineering', 2022),
       (4, 'Dave', 'Computer Science', 2022);

INSERT INTO courses (id, name, credits)
VALUES (1, 'Calculus', 4),
       (2, 'Physics', 4),
       (3, 'Computer Science', 4),
       (4, 'Electrical Engineering', 4);

INSERT INTO enrollment (student_id, course_id, grade)
VALUES (1, 1, 'A'),
       (1, 2, 'B'),
       (1, 3, 'A'),
       (2, 1, 'C'),
       (2, 4, 'A'),
       (3, 1, 'B'),
       (3, 2, 'A'),
       (4, 3, 'B');

Now we will excute all the basic comments on above data.

SELECT: retrieve data from the database

SELECT * FROM students;
SELECT name, major FROM students WHERE year = 2022;

INSERT: insert new data into the database

INSERT INTO students (id, name, major, year)
VALUES (5, 'Eve', 'Computer Science', 2021);

UPDATE: update existing data in the database

UPDATE students
SET major = 'Computer Engineering'
WHERE id = 5;

DELETE: delete data from the database

DELETE FROM students WHERE id = 5;

I hope this you have understood. If you have any questions then comment down. I will definitely try to solve it.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top