Mastering the Language of Data: SQL -1

Table of contents

No heading

No headings in the article.

Here we will understand basics of SQL, commands to create table, insert data into the table, fetch the data from the table.

So firstly let’s begin with CREATE, CREATE command is used to create a table in the database.

Syntax:

CREATE TABLE table_name
(column1 DataType,
column2 DataType,……)

Example:

CREATE TABLE Students
(name varchar,
 id int,
 marks int)

So here in create firstly we need to specify the Table Name then each of the column name with correct data type.

Next in line we have INSERT INTO, it is used to insert the values in the table.

Syntax:

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

Example:

INSERT INTO Students (name,id,marks)
VALUES
('Andrew',1,45),
('Stuart',2,49),
('Edward',3,78),
('Harry',4,89),
('Joe',5,65);

The above block of code will insert these 5 rows into the Students table.

Now let’s see statement to read the data from the table. For reading or fetching the data we use SELECT.

SELECT is the statement which we will use the most.

Syntax:

SELECT column1,column2,...
FROM table_name

Example:

SELECT name,id,marks
FROM Students
SELECT *
FROM Students

These both the SQL Queries gives same output.

SQL SELECT Statement Output

SELECT statement can also be extended to SELECT DISTINCT. Which returns only the distinct values. This comes handy if we have more than 1 values which are similar in a table and we only want value to be returned only once.

Syntax:

SELECT DISTINCT column1,column2,....
FROM table_name

Example:

SELECT DISTINCT name
FROM Students

The above query returns all the distinct names from the students table.

SELECT DISTINCT name,marks
FROM Students

The above query returns all the distinct name, marks pair from the students table.

This is all for this post. More statements ahead.

Thanks for reading!!!