×

SQL Queries

In a database, queries are used to request the result set of data from the table or action on the records.

A Query can answer your simple or complicated question, perform the operation, combine records from the different tables, add records into the Database or the table, delete records, and modify records from the Database or the table.

In an SQL database, we can execute multiple queries such as insert data into the table, modify the data, delete the table, select data from the table, modification into the table, etc.

SQL queries that we execute on the Database and table are as follows:

  • SQL CREATE query
  • SQL INSERT query
  • SQL SELECT query
  • SQL UPDATE query
  • SQL DELETE query
  • SQL DROP query
  • SQL TRUNCATE query
  • SQL ALTER query

We will look at each query one by one with syntax and examples.

SQL CREATE query:

SQL CREATE query is used to create a database, table, index, view, trigger, sequence, and function.

The syntax for SQL CREATE Database:

CREATE DATABASE Database_Name;

CREATE DATABASE is a keyword used to create a Database followed by a database name.

Syntax for SQL CREATE Table:

CREATE TABLE Table_Name( Column_Name_1 data type column_constraint, Column_Name_2 data type  column_constraint, Column_Name_3 data type  column_constraint), Column_Name_4 data type  column_constraint), Column_Name_5 data type  column_constraint); 

CREATE TABLE is a keyword used to create a table followed by table name and column definition.

We will create one table named Diploma_Student with Nine columns:

CREATE TABLE Diploma_Student(Student_Id int NOT NULL, Student_Name varchar(40) NOT NULL, First_Sem int, Second_Sem int, Third_Sem int, Fourth_Sem int, Fifth_Sem int, Sixth_Sem int, Total int, PRIMARY KEY(Student_Id));

In the above example, we have created a Diploma_Student table with following columns.

We will now use the DESC keyword followed by table name:

FieldTypeNullKeyDefaultExtra
Student_Idint(11)NOPRINULL 
Student_Namevarchar(40)NO NULL 
First_Semint(11)YES NULL 
Second_Semint(11)YES NULL 
Third_Semint(11)YES NULL 
Fourth_Semint(11)YES NULL 
Fifth_Semint(11)YES NULL 
Sixth_Semint(11)YES NULL 
Totalint(11)YES NULL 
SQL Queries

SQL INSERT query:

We use the SQL INSERT query to add records in the empty table, or add some extra records in the existing table.

The syntax for SQL INSERT Query:

INSERT INTO Table_Name Values(Value1, Vlaue2, Value3, Value4, Value5);

The above query is used to add records in all table columns.

If you want to add records in the selected columns, use the following insert syntax:

INSERT INTO Table_Name(Column1, Column2, Column3, Column4) Values(Value1, Value2, Value3, Value4);

In the following example, we will add six records in the newly created table Diploma_Student:

INSERT INTO Diploma_Student VALUES(202111, 'Vaishnavi Patil', 94, 91, 88, 85, 95, 92, 91);

INSERT INTO Diploma_Student VALUES(202112, 'Vaibhav Lokhande', 85, 90, 92, 80, 85, 82, 86);

INSERT INTO Diploma_Student VALUES(202113, 'Yash Dhull', 90, 88, 94, 87, 85, 90, 89);

INSERT INTO Diploma_Student VALUES(202114, 'Sonali Patole', 95, 90, 92, 88, 92, 90, 91);

INSERT INTO Diploma_Student VALUES(202115, 'Axar Patel', 85, 80, 82, 86 , 92, 84, 85);

INSERT INTO Diploma_Student VALUES(202116, 'Meena Mishra', 78, 75, 80, 74 , 85, 77, 78)

We have inserted six records into the Diploma_Student table.

SQL SELECT query:

SQL SELECT query is used to retrieve records from the table. We can fetch all the records from the table or retrieve the selected records according to the conditions using the SELECT query. For example, we will use the WHERE clause with the SELECT query to fetch students records whose total is greater than 85.

Syntax of SQL SELECT Query:

SELECT * FROM Table_Name;

The above syntax is used to fetch all the records from the table.

SELECT * FROM Table_Name WHERE conditions;

The above syntax is used to fetch specific records from the table.

We will fetch all the records from the Diploma_Student table using the following query:

SELECT * FROM Diploma_Student;

The Output of the SELECT Query is as follows:

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotal
202111Vaishnavi Patil94918885959291
202112Vaibhav Lokhande85909280858286
202113Yash Dhull90889487859089
202114Sonali Patole95909288929091
202115Axar Patel85808286928485
202116Meena Mishra78758074857778
SQL Queries

SQL UPDATE Query:

SQL UPDATE Query is used to modify the data of a table.

Syntax of SQL UPDATE query:

UPDATE Table_Name SET Column_Name = Value WHERE Condition;

Above syntax updates the records based on the given condition.

We will modify the student marks of the sixth sem whose student id is 202116.

UPDATE Diploma_Student SET Sixth_Sem = 82 WHERE Student_Id = 202116;

We will now execute the SELECT query to check whether Diploma_Student records are updated or not.

SELECT * FROM Diploma_Student WHERE Student_Id = 202116;  
Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotal
202116Meena Mishra78758074858279
SQL Queries

As we can see, records are updated successfully.

SQL ALTER Query:

In SQL, ALTER Query adds a column, deletes a column, and modifies a column in a table. SQL ALTER query is also used to rename a column, add, and drop constraints.

Syntax of how to add a column in a table:

ALTER TABLE Table_Name ADD Column_Name Data type;

Syntax of how to Drop column in a table:

ALTER TABLE Table_Name DROP Column Column_Name;

Syntax of how to modify column in a table:

ALTER TABLE Table_Name MODIFY Column_Name data type;

SQL DELETE Query:

SQL DELETE query is used to remove the data from the table.

Syntax of SQL DELETE Query:

DELETE FROM Table_Name;

The above syntax is used to remove all the records from the table. If you want to delete specific data from the table, use the below query:

DELETE FROM Table_Name WHERE condition;

If we want to delete records from the Diploma_Student table whose Student id is 202116, then, execute the following query:

DELETE FROM Diploma_Student WHERE Student_Id = 202116;

We will now execute the SELECT query to check whether Diploma_Student records are deleted or not.

SELECT * FROM Diploma_Student;
Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotal
202111Vaishnavi Patil94918885959291
202112Vaibhav Lokhande85909280858286
202113Yash Dhull90889487859089
202114Sonali Patole95909288929091
202115Axar Patel85808286928485
SQL Queries

SQL TRUNCATE Query:

SQL TRUNCATE Query is used to delete all the records of a table. It keeps the table structure as it is.

Syntax of SQL Truncate Query:

TRUNCATE TABLE Table_Name;

We will now remove all the records of the Diploma_Student table using the truncate query:

TRUNCATE TABLE Diploma_Student;

After executing the truncate query, if we execute the SELECT query message displays an empty set or no result.

SQL DROP Query:

SQL DROP query is used to delete the record and table from the structure. It is also used to drop the Database from the system.

Syntax of SQL DROP query:

DROP TABLE Table_Name;

We will now drop the Diploma_Student table from the Database.

DROP TABLE Diploma_Student;

It will delete all the records of the table with table structure.


Related Topics

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.

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 CASE

This page contains all the information about SQL CASE. The CASE is an If-Else type of logical query used in the statement. The CASE in Structured Query Language is similar...

5 minutes read.

TCL Commands in SQL

In Structured Query Language, TCL is an abbreviation for Transaction Control Language. A single unit of work in a database is formed after the consecutive execution of commands is known...

6 minutes read.

Introduction to SQL

SQL Introduction SQL is Standard Query Language. This language is used to communicate or interact with database. In other words, SQL is used to access and manage data or information...

2 minutes read.

How to Add Comments in SQL?

How to Add Comments in SQL Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections...

2 minutes read.

SQL Operators

Arithmetic Operators Arithmetic operators are +, -, *, /, % performs addition, subtraction, multiplication, division, modulo respectively. Example:   Select 100+222; Select salary+100 From teacher Where teacher_id=1; Following output screen shows different arithmetic operations. We...

2 minutes read.

SQL Formatter

As a beginner, one does not focus much on Formatting the queries while writing their code. This leads to a great confusion while referring the code in future. For a...

5 minutes read.

SQL WHERE Statement

SQL WHERE Statement Introduction WHERE clause is used to include a condition while fetching data from tables.When you have to specify a condition that has to be obeyed while data is...

9 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 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.

How to use LIKE in SQL

In this SQL article, we will learn and understand how to use LIKE to the columns in the SQL tables. What is Like? Like is an operator in the SQL. It is...

7 minutes read.

How to use COUNT in SQL?

How to use COUNT in SQL Introduction COUNT( ) is an aggregate function in SQL.This function counts the number of records in a table if the condition is not specified.If the condition...

4 minutes read.

Commit and Rollback in SQL

In Structured Query Language, we have Data Definition Language (DDL) and Data Manipulation Language (DML) commands the same way we have Transaction Control Language (TCL) commands in the Structured Query...

4 minutes read.

SQL Comparision Operator

The Comparison Operator compares different data of the Structured Query Language table and checks whether the data are the same, less than, greater than, less than, or greater than equal....

14 minutes read.

Codd’s Rules in SQL

Codd’s Rules Dr. Edgar F. Codd, in 1985, laid down 13 fundamental rules after doing large-scale research on the Relational Model of databases. According to him, every database must follow these...

3 minutes read.

How to add column in table in SQL ?

How to add column in table in SQL  Introduction To add a column in already created table, one needs to use the ALTER command along with the ADD clause.If in the query,...

7 minutes read.

SQL Data Control Language

Data Control Language decides to whom should (which user) permit access privileges. GRANT and REVOKE are the commands of DCL. GRANT: It gives privileges to user. REVOKE: It takes back privileges from granted...

1 minute read.

Where vs Having

Difference Between Where and Having The WHERE and HAVING clauses in SQL are used to filter records stored in tables in the databases. The dissimilarities between the WHERE and HAVING clause...

3 minutes read.

SQL Topological Sorting

It is for Directed Acyclic Graph, which linearly describes the ordering of graphs. It is not possible for all graphs, it is possible for only Directed acyclic graphs. Applications of Topological...

3 minutes read.