×

SQL Syntax

In this tutorial, we will help you understand about the different SQL Syntax concepts with the help of an example.

Every Structured Query Language starts with Keywords like select, insert, update, delete, drop, alter, create, use, show, and each statement end with a semicolon (;).

Even Semicolon separates each SQL statement in the database System when multiple SQL statements are executed.

We can execute Structured Query Language Keywords in both cases uppercase and lowercase, i.e., SQL is case insensitive.

Let’s learn about the SQL Syntax with the help of an example

1. SELECT Statement

The SELECT statement is used to fetch the record from a database.

The syntax of the SELECT statement is as follows:

SELECT * FROM table-name;

Example of the SELECT statement

SELECT * FROM Students;
Student_IdStudent_NameStudent_Dept
102Deepak ChaudhariCivil Engineering
103Megha SonjeInformation Technology
104Ketan BadhanMechanical Engineering
105Chetan SangaleMechanical Engineering
106Anu PriyaComputer Engineering
107Daya JainChemical Engineering
SQL Syntax

The above syntax displays all the records from the mentioned table name.

Use the below syntax to display the records from the specific column.

SELECT column_1, column_2, column_3 FROM table-name;

Example:

SELECT Student_Id, Student_Name FROM Students;

The output of the above query is as follows:

Student_IdStudent_Name
102Deepak Chaudhari
103Megha Sonje
104Ketan Badhan
105Chetan Sangale
106Anu Priya
107Daya Jain
SQL Syntax

2. CREATE Statement

CREATE statement is used to create a new table in the database with the specific name of the table and the name of each column followed by their data type in the table.

The syntax of the CREATE statement is as follows:

CREATE table table_name( Column_1 data type, Column_2 data type, Column_3 data type );

Example of create statement

CREATE table Students( Student_Id int NOT NULL, Student_Name VarChar(50) NOT NULL, Student_Dept VarChar(50) NOT NULL, PRIMARY KEY(Student_Id));

The output of the above query is as follows:

FieldTypeNullKeyDefaultExtra
Student_Idint(11)NOPRINULL
Student_Namevarchar(50)NONULL
Student_Deptvarchar(50)NONULL

3. DELETE Statement-

DELETE Statement is used to remove data from the database

The syntax of the above query is as follows:

DELETE FROM table_name;

Example of DELETE Statement

DELETE FROM Students;

The above query removes all the records from the mentioned table.

The below DELETE statement syntax is used to delete specific data from the table.

DELETE FROM table_name WHERE condition;

Where clause specifies which data or record should be deleted

Example:

DELETE FROM Student_Course WHERE Scourse_Id  = 6;

The output of the above query is as follows:

Student_IdStudent_Name
1102
2102
1104
3103
5106
7107
SQL Syntax

4. ALTER Statement

The ALTER query is used to add new fields in an already existing table, remove fields, modify the fields and their data type, add constraints, drop the constraints, to rename the field name in an already existing table.

The syntax of the ALTER statement is as follows:

ALTER TABLE table_name ADD column_1 data type;

The above syntax adds a new column in an already existing table.

Example

ALTER TABLE Students ADD DateofBirthdate;

The syntax for to DROP column using the ALTER query is as follows:

ALTER TABLE table_name DROP COLUMN column_1;

Example:

ALTER TABLE Students DROP COLUMN Student_City;

The syntax for to MODIFY column using the ALTER query is as follows:

ALTER TABLE table_name MODIFY COLUMN datatype;

Example:

ALTER TABLE Students MODIFY COLUMN DateofBirthyear;

The output of the above query is as follows:

SQL Syntax

5. INSERT INTO Statement

INSERT INTO statement adds new data to existing data.

The syntax of the INSERT INTO statement is as follows:

INSERT INTO table_name (Column_1, Column_2, Column_3) VALUES (value_1, value_2, value_3);

Example of INSERT INTO statement

INSERT INTO Students (Student_Id, Student_Name, Student_Dept) VALUES (108, ’Komal Maheshwari’, ‘Computer Engineering’);

The output of the above query is as follows:

Student_IdStudent_NameStudent_Dept
102Deepak ChaudhariCivil Engineering
103Megha SonjeInformation Technology
104Ketan BadhanMechanical Engineering
105Chetan SangaleMechanical Engineering
106Anu PriyaComputer Engineering
107Daya JainChemical Engineering
108Komal MaheshwariComputer Engineering
SQL Syntax

6. UPDATE Statement                                      

The UPDATE query is used to modify the specific data or all the data in a table.

The syntax of the UPDATE statement is as follows:

UPDATE table_name SET column_1 = value WHERE condition;

Example of UPDATE Statement

UPDATE Student SET Student_Name = ‘Mangesh Maheshwari’ WHERE Student_Name = ‘Sonal Maheshwari’;

The output of the above query is as follows:

SQL Syntax

7. DROP TABLE Statement

The DROP table query removes the table data and table schema from the database.

The syntax of the DROP TABLE query is as follows:

DROP TABLE table_name;

Example of DROP TABLE Statement

DROP TABLE Students;

The output of the above query is as follows:

SQL Syntax

8. TRUNCATE TABLE Statement

The TRUNCATE TABLE Statement removes the records from the table without disturbing the table schema.

The syntax of the TRUNCATE TABLE statement is as follows:

TRUNCATE TABLE table_name;

Example of TRUNCATE TABLE Statement

TRUNCATE TABLE Students;

The output of the above query is as follows:

SQL Syntax

9. CREATE DATABASE Statement

The CREATE DATABASE statement is used to create a new database

The syntax of the CREATE DATABASE is as follows:

CREATE DATABASE DataBase_Name;

Example of CREATE DATABASE statement

CREATE DATABASE College;

The output of the above query is as follows:

SQL Syntax

10. DROP DATABASE Statement

The DROP DATABASE statement is used to remove an already existing database.

The syntax of the DROP DATABASE statement is as follows:

DROP DATABASE DataBase_Name;

Example of DROP DATABASE statement

DROP DATABASE College;

The output of the above query is as follows:

SQL Syntax

11. SELECT DISTINCT Statement

The SELECT DISTINCT statement returns a unique value in the specified columns.

The syntax of SELECT DISTINCT statement is as follows:

SELECT DISTINCT column_1, column_2 FROM table_name;

Example

SELECT DISTINCT Student_Dept FROM Students;

The output of the above query is as follows:

SQL Syntax

12. INNER Join Statement

The INNER JOIN statement is used to join the table and display similar records in the tables.

The syntax of the INNER JOIN Statement is as follows:

SELECT table_name_1.column_1 FROM table_name_1 INNER JOIN table_name_2 ON table_name_1.column_1 = table_name_2.column_1;

Example

SELECT Students.Student_name FROM Students INNER JOIN Student_Course ON Students.Student_Id = Student_Course.Student.Id;

The output of the above query is as follows:

SQL Syntax

13. LEFT JOIN Statement

The LEFT JOIN statement will return all records from the left table and the common records from the right table. If records from the right table are not common, it will return a null value.

The syntax of the LEFT JOIN Statement is as follows:

SELECT table_name_1.column_1 FROM table_name_1 LEFT JOIN table_name_2 ON table_name_1.column_1 = table_name_2.column_1;

Example

SELECT Students.Student_Name, Student_Course.SCourse_Id FROM Students LEFT JOIN Student_Course ON Students.Student_Id = Student_Course.Student_Id;

The output of the above query is as follows:

SQL Syntax

14. RIGHT JOIN Statement

The Right Join query display all the records of the table on the right side of the join and common records for the table on the left side of the join. The result-set will contain null for the records for which there is no similar record on the left side.

The syntax of the RIGHT JOIN Statement is as follows:

SELECT table_name_1.column_1 FROM table_name_1 RIGHT JOIN table_name_2 ON table_name_1.column_1 = table_name_2.column_1;

Example

SELECT Students.Student_Name, Student_Course.SCourse_Id FROM Students RIGHT JOIN Student_Course ON Students.Student_Id = Student_Course.Student_Id;

The output of the above query is as follows:

SQL Syntax

Related Topics

SQL WHERE Clause

The WHERE clause is a search filter that returns only true records. The WHERE clause chooses the selected records based on the given conditions. The WHERE clause is used with...

4 minutes read.

SQL SubQuery

The Sub-query in the SQL is the inner query placed or positioned inside another query, which is also known is the outer query. The inner query is embedded in the...

6 minutes read.

SQL FULL JOIN

In this section, we will help you understand the concept of the SQL FULL Join clause with a few examples. The SQL FULL Join query is executed to display the integrated...

3 minutes read.

SQL Aggregate Functions

In this tutorial, we will learn and understand the SQL Aggregate Functions concept with the help of examples. SQL Aggregate Function is a function used to perform calculation operations on one...

4 minutes read.

Types of SQL Commands

The Structured Query Language is used to deal with structured data. The data which are stored in the form of tables are structured data. These SQL commands store records or...

10 minutes read.

SQL Temporary Tables

Temporary Table: The Temporary tables are generally created in TempDB. If the last connection is terminated then the tables are deleted automatically. These are generally used to store and process the...

4 minutes read.

How to use GROUP BY clause in SQL

In this SQL article, we will learn about the GROUP BY clause and how to use it in SQL. We will also discuss using the GROUP BY clause with the...

6 minutes read.

Save Point in SQL

In SQL, the classification is done into 4 languages. They are Data Definition Language (DDL)Data Manipulation Language (DML)Transaction Control Language (TCL)Data Control Language (DCL) Save Point falls under the Transaction Control Language....

4 minutes read.

SQL Wildcards

SQL WILDCARD CHARACTERS: SQL wild card character is used to substitute zero or more characters of a string. These characters are used with the “LIKE” operator. Wild Card Characters in SQL: %_[]^- “%” : It is...

3 minutes read.

SQL KEYS

SQL KEYS are single or multiple attributes used to get data from the table according to the requirement or condition. They can also be used to set up relationships amongst...

3 minutes read.

SQL Data Definition Language

Data definition language directly effects on the structure/schema of the database. CREATE, ALTER, DROP are the commands of DDL.CREATE: Creates new database, table, or view of table.ALTER: Modifies the database or...

3 minutes read.

SQL UPDATE

SQL UPDATE The SQL UPDATE statement is utilized to update and modify the records present into a database. It is used to change the already existing records stored in the tables...

3 minutes read.

SQL Data Types

In SQL DATA TYPES, each column holds value. Data types can be an integer type, character type, etc., and such data is stored in the database table. The data type is...

4 minutes read.

What is SQL Injection?

Introduction to SQL Injection SQL injection is a vulnerability or a technique that might destroy the database of a website or a web application. It is one of the most widely...

4 minutes read.

SQL Arithmetic Operators

This page contains all the information, learn about SQL Arithmetic Operators concept in the SQL table with the help of examples. The Arithmetic Operators is used to perform mathematical calculations on...

5 minutes read.

Drop vs Truncate in SQL

In this article, we will learn and understand the Drop and Truncate commands and the difference between these two commands. What is Drop Command? Drop is a Data Definition Language command in...

6 minutes read.

How to compare date in SQL

In this section, we will learn about how dates can be compared in SQL. We can compare any random date with another date stored in a column of a table.This comparison...

4 minutes read.

SQL Aliases

SQL Aliases: These are used to give a temporary name for a column or table. These are used to make tables or columns more readable. These are used to rename the table...

2 minutes read.

Pattern Matching in SQL

The LIKE in the Structured Query Language is a Logical Operator. The SQL LIKE is used within the WHERE clause in the SQL. This Like Operator is used with the...

5 minutes read.

SQL Alter Table

In Structured Query Language, if you want to add columns in an existing table, then modify the table, or delete columns from the table. All these operations are allowed only...

7 minutes read.