BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

SQL Syntax

Last Updated: November 27, 2018 by Chaitanya Singh | Filed Under: SQL

A database is a organized collection of data. Data is stored in relational database in form of tables. To create, retrieve, update and delete from relational database, we use SQL queries. In this guide, we will learn the SQL Syntax.

SQL Statements

SQL statement tells the database that what information you would like to retrieve or what operation you want to perform on the data.

For example:
Consider this table: STUDENT

STUDENT_NAME     STU_AGE     STU_ADDRESS
------------     -------     ------------
Ajeet             30          Chennai
Chaitanya         31          Noida
Steve             29          Agra
Rahul             30          Gurgaon

SQL statement to fetch STUDENT_NAME from the table STUDENT:

SELECT STUDENT_NAME FROM STUDENT;

To fetch the complete table:

SELECT * FROM STUDENT;

SQL is NOT case sensitive

SQL is not a case sensitive language. For example: Both the following statements would work fine and produce the same output:

select * from student;

&

SELECT * FROM STUDENT;

Semicolon

As we have seen above, we have to end the SQL statement with a semi colon, it tells the RDBMS that this is a complete SQL statement. We can write more than one SQL statements together but we have to end each one of them with semicolon so that the database management system knows that they are different SQL statements. This way RDBMS can serve more than one SQL queries in a single database call.

Syntax of Most Important SQL Commands

Important Note: The following commands are covered separately on our website so you don’t have to get into the details now, just go through them once and practice them after you have gone through the individual tutorial of each of them.

SQL SELECT Statement

To fetch the data from table

SELECT column_name1, column_name2....column_nameN
FROM   table_name;

SQL WHERE Clause

TO fetch the specific rows from the table that meets the given condition.

SELECT column_name1, column_name2....column_nameN
FROM   table_name
WHERE  CONDITION;

SQL ORDER BY Clause

To fetch the records in a particular order

SELECT column_name1, column_name2....column_nameN
FROM table_name
ORDER BY column_name1, column_name2, ... {ASC|DESC};

SQL GROUP BY Clause

SELECT COUNT(column_name) FROM   table_name
WHERE  CONDITION 
GROUP BY column_name;

SQL DISTINCT Clause

To fetch distinct values of column from the table.

SELECT DISTINCT column_name1, column_name2....column_nameN
FROM table_name;

SQL HAVING Clause

SELECT SUM(column_name)
FROM table_name
WHERE CONDITION
GROUP BY column_name
HAVING (arithmetic function condition);

SQL CREATE TABLE Statement

To create the table in the database.

CREATE TABLE table_name(
column_name1 data_type,
column_name2 data_type,
.....
.....
column_nameN data_type,
PRIMARY KEY(column(s))
);

SQL DESC Statement

DESC table_name;

SQL INSERT INTO Statement

To insert a record into the table.

INSERT INTO table_name( column_name1, column_name2....column_nameN)
VALUES (value_1, value_2.....value_N);

SQL UPDATE Statement

To update data in table.

UPDATE table_name
SET column_name1 = value_1, column_name2 = value_2....column_nameN=value_N 
WHERE  CONDITION;

SQL DELETE Statement

To delete rows from table.

DELETE FROM table_name
WHERE CONDITION;

SQL DROP TABLE Statement

To delete the table completely from database.

DROP TABLE table_name;

SQL TRUNCATE TABLE Statement

To delete all the records from the table.

TRUNCATE TABLE table_name;

ADD, DROP or MODIFY column of table

ALTER TABLE table_name {ADD|DROP|MODIFY} column_name {data_type};

SQL CREATE INDEX Statement

CREATE UNIQUE INDEX index_name
ON table_name ( column_name1, column_name2,...column_nameN);

Dropping Index of table

ALTER TABLE table_name
DROP INDEX index_name;

Rename Table name

ALTER TABLE table_name RENAME TO new_table_name;

SQL AND/OR Clause

SELECT column_name1, column_name2....column_nameN
FROM   table_name
WHERE  CONDITION_1 {AND|OR} CONDITION_2;

SQL COUNT Clause

SELECT COUNT(column_name)
FROM   table_name
WHERE  CONDITION;

SQL IN Clause

Selects rows based on the column values in a given set of values.

SELECT column_name1, column_name2....column_nameN
FROM   table_name
WHERE  column_name IN (value_1, value_2,...value_N);

SQL BETWEEN Clause

Selects rows based on the column values in a given range.

SELECT column_name1, column_name2....column_nameN
FROM   table_name
WHERE  column_name BETWEEN value_1 AND value_2;

SQL USE Statement

To select a particular database.

USE database_name;

SQL CREATE DATABASE Statement

To create database.

CREATE DATABASE database_name;

SQL DROP DATABASE Statement

Delete the entire database.

DROP DATABASE database_name;

SQL COMMIT Statement

To commit the changes permanently to the database.

COMMIT;

SQL ROLLBACK Statement

To rollback the changes made to the database to a previous checkpoint.

ROLLBACK;
❮ PreviousNext ❯

Top Related Articles:

  1. Introduction to SQL
  2. SQL CREATE DATABASE Statement
  3. SQL – DROP Table Statement to delete the entire table
  4. DELETE Query in SQL
  5. SQL SELECT TOP Statement

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

SQL Tutorial

  • SQL Tutorial
  • SQL Introduction
  • SQL Syntax
  • SQL Data Types

SQL Database

  • SQL CREATE DB
  • SQL DROP DB
  • SQL Rename DB
  • SQL USE DB

SQL Queries

  • SQL Select
  • SQL Select Distinct
  • SQL Select Count
  • SQL Select Top
  • SQL Where
  • SQL AND, OR & NOT
  • SQL Order By
  • SQL Insert Into
  • SQL Insert Into SELECT
  • SQL Select Random
  • SQL Alias
  • SQL NULL Check
  • SQL Update
  • SQL Delete
  • SQL MIN, MAX
  • SQL SUM
  • SQL AVG

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap