DEV Community

Swetha G
Swetha G

Posted on

SQL Masterclass

**What is SQL?
Databases are used to store vast amounts of data. Just like how programming languages are used to communicate with computers, SQL is the language used to communicate with relational databases.

What is a query?
A query is simply a question you ask the database—give me these columns, from this table, with these conditions.

Syntax in SQL
Just like grammar rules in a language, SQL also has rules for writing a query. Think of a SQL statement as a small recipe:

SELECT -- columns or calculations
FROM -- table or view name
WHERE -- optional filter
GROUP BY -- optional grouping
HAVING -- optional, after grouping
ORDER BY -- optional sorting
LIMIT -- optional cap

**Types of SQL Operations
There are a few major types of operations performed on databases. Based on these, SQL statements are categorized as:

**SQL Categories
Data Definition Language (DDL)
Data Manipulation Language (DML)
Data Query Language (DQL)
Data Control Language (DCL)
Transaction Control Language (TCL)
This will make more sense as you learn each of them below.

CREAT:
CREATE is the keyword that tells the database you want to create something. In this case, we will use it to create a table.

To create a table, you need three primary details:

CREATE Requirements List
The name of the table
The columns/fields to include
The data type of each column
Once this is decided, the syntax is:

CREATE TABLE table_name (
column1 data_type,
column2 data_type,
...
);

Example:

-- Students table
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100),
department VARCHAR(10),
year INT,
cgpa DECIMAL(3,2),
city VARCHAR(50),
admission_date DATE
);

INSERT:

Now that we have the table, let's insert data into it. You need to specify:

INSERT Requirements
the columns you're filling, and
the values, in the same order.
INSERT INTO are the keywords used to insert data into a table.

Basic syntax:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

Example:
-- Insert a single student
INSERT INTO students (name, email, department, year, cgpa, city, admission_date)
VALUES ('Rahul Sharma', 'rahul@college.edu', 'CSE', 3, 8.5, 'Delhi', '2022-08-01');

-- Insert multiple students at once
INSERT INTO students (name, email, department, year, cgpa, city, admission_date)
VALUES
('Priya Patel', 'priya@college.edu', 'CSE', 3, 9.2, 'Mumbai', '2022-08-01'),
('Amit Kumar', 'amit@college.edu', 'ECE', 2, 7.8, 'Bangalore', '2023-08-01'),
('Sneha Reddy', 'sneha@college.edu', 'CSE', 4, 8.9, 'Hyderabad', '2021-08-01');

SELECT
Wait, now that we have the data in the database (DB), let's play around with it. Now, let's first read the data that we inserted. To read the data, we have the SELECT keyword, where you need to mention the following:

SELECT Components
The table from which the data needs to be fetched
The columns from which data needs to be fetched
Syntax:
SELECT columnName... FROM tableName;
Example:
SELECT name, email, dept FROM Students;

WHERE
We know how to fetch the data, let's add more power to it. If you want to fetch data based on a condition that's where filters come in. WHERE lets you specify a condition that each row we fetch must satisfy.

What WHERE does
Filters rows based on the condition specified. For example, you might fetch only students with a CGPA > 8, students enrolled in a particular course, or students taught by a professor named Rajesh.

Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE ;

Top comments (0)