Back to all posts

SQL: SQL Basics – 1 (CRUD)


CRUD Operations:

  • SELECT: Retrieve data.
  • INSERT: Add new data.
  • UPDATE: Modify existing data.
  • DELETE: Remove data.

What is SQL

SQL (structured Query Language) is a programing language designed for managing designed for managing data in relational database.

  1. How to create a table?
  2. How to select data from the table?
  3. How to insert data to the table?
  4. How to update data from the table?
  5. How to delete the data from the table?

How to create a table

To create a table in MySQL, use the CREATE TABLE statement. Define the table name and its columns, along with their data types and constraints.

CREATE TABLE table_name (
   column1 datatype constraints,
   column2 datatype constraints,
   column3 datatype constraints,
   ...
);

Example:

CREATE TABLE students (
   id INT AUTO_INCREMENT PRIMARY KEY,
   name VARCHAR(100) NOT NULL,
   age INT,
   enrollment_date DATE
);

How to Select Data from the Table

To retrieve data from a table, use the SELECT statement.

SELECT column1, column2, ... FROM table_name WHERE condition;

Example:

// Select all columns:
SELECT * FROM students;
// Select Specific columns:
SELECT name, age, FROM students WHERE age > 18

How to Insert Data into the Table

To insert data into a table, use the INSERT INTO statement. Specifiy the columns and the corresponding values.

INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);

Example:

INSERT INTO students (name, age, enrollment_date) VALUES ('Chethan', 24, '2024-11-11');

How to Update Data in the Table

To update existing data, use the UPDATE statement with a SET clause to specify new values and a WHERE clause to target specific rows.

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Example:

UPDATE students
SET age = 24
WHERE name = "Chethan";

How to Delete Data from the Table

To delete data from a table use the DELETE statement. Be cautions when using it, as it removes data permanently.

DELETE FROM table_name WHERE condition;

Example:

// Delete specific rows:
DELETE FROM students WHERE age < 24;
//Delete all rows
DELETE FROM students;