×

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

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

SQL CONSTRAINTS

SQL Constraints specifies the rules/limitations/restrictions for data present in table. SQL Constraints are specified at the time of table creation or after table creation using ALTER command. There are two...

5 minutes read.

SQL SELECT AVG

In this tutorial, we will learn about the aggregate function name avg() function concept in SQL with the help of examples. The AVG() function is one of the aggregate functions in...

3 minutes read.

Difference between Delete, Drop and Truncate in SQL

What is the Delete command in SQL? In DML (Data Manipulation Language), we use the delete command that allows us to delete the some entries and modify the databases in SQL....

4 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 SELECT WHERE Clause

In this SQL section, we will help you understand the concept of the SQL SELECT WHERE clause with a few examples. The WHERE clause in SELECT query displays those records as...

5 minutes read.

DML Commands in SQL

DML is an abbreviation of Data Manipulation Language. Data Manipulation Language commands in Structured Query Language manipulate the data in the database. DML commands are used to retrieve records, add records,...

4 minutes read.

SQL HAVING Clause

In this tutorial, we will understand the HAVING clause concept in SQL with the help of examples. The HAVING Clause in the SQL is used as we cannot use the WHERE...

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

Where Condition in SQL

WHERE clause is used with the Data Manipulation Language Command in SQL, the Data Manipulation Language commands are the SELECT statement, UPDATE statement, and DELETE statement. WHERE clause condition is an...

5 minutes read.

SQL DROP Database

This tutorial will help us understand how to drop a database from the SQL Server with a few examples. The DROP command removes all the objects stored in the database and...

4 minutes read.

SQL Primary Key

A field, which contains unique data in a table, is called a PRIMARY KEY (PK). It means, a PRIMARY KEY field must contain unique data in the table. A PRIMARY KEY...

4 minutes read.

SQL Right Join

The SQL Right Join query displays all the table records and similar records from the left table. The query display zero records if it doesn’t find any similar records. If...

4 minutes read.

SQL Inner Join

In Structured Query Language, the most used join query is the Inner join query. Inner join query retrieves the records from one or more tables with similar data or records. The...

4 minutes read.

SQL CREATE TABLE

In SQL tutorial, we learned and created different databases. To stores data in databases, we need to create a table. To create the table, we need to use CREATE TABLE...

5 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 Handling Duplicate

Removing Duplicates using DISTINCT Keyword: By using the DISTINCT keyword in SQL we can remove duplicate characters from tables or databases. A table contains more duplicate values and duplicate values can cause...

4 minutes read.

How to Create Temporary Table in SQL?

How to Create Temporary Table in SQL  Introduction to Temporary Tables Temporary table is a table which is used to store temporary data that can be used further in the same client...

3 minutes read.