0% found this document useful (0 votes)
172 views58 pages

18 Dec 2021 - Database (SQL)

The document provides information about functional testing and database testing. It discusses testing the front end of a sign up form and validating the correctness and completeness of data in the customer sign up database table. It also covers database concepts like structured vs unstructured data, database management systems, SQL commands for querying, manipulating, and defining database structure. Examples are provided for various SQL statements like SELECT, WHERE, ORDER BY, DISTINCT and others.

Uploaded by

akshay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
172 views58 pages

18 Dec 2021 - Database (SQL)

The document provides information about functional testing and database testing. It discusses testing the front end of a sign up form and validating the correctness and completeness of data in the customer sign up database table. It also covers database concepts like structured vs unstructured data, database management systems, SQL commands for querying, manipulating, and defining database structure. Examples are provided for various SQL statements like SELECT, WHERE, ORDER BY, DISTINCT and others.

Uploaded by

akshay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 58

Functional Testing

1. B
2. I
3. E
4. D/B
5. S
6. C

Database Testing

Sign Up form – Check on UI (Front End)

FN- M

LN- C

Email- CM

Mb. No-12

Address-Pune

Submit

“Sign up is successful” – we can check for success message

Database-

Customer Sign Up Table

- Validate correctness & completeness

Database/ Backend Testing

Database- storage location – collection of related data

1. Structured data- Facebook, filpkart, amazon, MSEB, MPSC, IRCTC


2. Unstructured data- Random data- social media post- Data cannot be stored in DB

Database management system (DBMS)

- To perform any operations on the data we need the DBMS


- Different database- Oracal, sql server, My Sql, MangoDB
When we deal with structured data we use RDBMS

What is SQL-

SQL – Structured Query Language

It is designed for managing data in a RDBMS

Pronounced- SQL or See-Qwell

SQL- is a database language / not the database

SQL- used

- Database creation
- deletion
- Fetching row
- Modifying row

Why SQL is required

- To create database/table
- To insert record
- To update record
- To delete records
- To retrieve data

SQL Syntax

SQL follows some unique set of rules & regulation called syntax

SQL- is not case sensitive. Generally SQL keywords are written in Uppercase

SQL Statement

SQL statement are stared with any of the SQL command/keyword

Iike- SELECT, INSERT, UPDATE, DELETE, ALTER, DROP


Statement ends with a semicolon (;)

Ex. SELECT “Column name” FROM “table name”;

Why use semicolon?

Used to separate SQL statement

SQL Commands / Keyword

Select- extract data from the database

Update- update data in database

Delete-

Create table-

Alter table-

Drop table-

Inert in to –

SQL Commands- are instruction. It is used to communicate with the database also used to
perform specific task

Types of SQL commands

Five types of SQL commands

1. DDL- data definition language


2. DML- data manipulation language
3. DCL- data control language
4. TCL – transaction control language
5. DQL- data query language

DDL- data definition language

- Changes the structure of the table like creating a table, delete a table, altering table etc….
- All DDL command- auto committed – means – it permanently save all the changes in the
database
- DDL commands
1. Create
2. Alter
3. Drop
4. Truncate

DML- data manipulation language

- DML commands used to modify the database


- DML- not auto committed- means it can’t permanently save all the changes in the
database
- They can be rollback
- DML – commands
1. Insert
2. Update
3. Delete

Data query language

- DQL is used to fetch the data from the database/ table


- DQL command
1. Select

Database- collection of data- storage – no of table – row (records) & column (field)

Ex. Sign up, login credentials, orders, employee info, student’s registration

Table Name- Sign Up – front end- UI- ID,FN,LN,MObNo,EmailID, City- submit- successful

ID FN LN MobNo EmailId City

1 Rahul Gandhi 1111 [email protected] Pune

2 Arvind Kejriwal 2222 [email protected] Mumbai

3 Anna Hajare 3333 [email protected] Pune

4 Sharad Pawar 4444 [email protected] Baramati

5 Soniya Gandhi 5555 [email protected] Pune


1. SQL SELECT statement
- Fetch the data from the database
- Retrieve the data from the database
- Following sql statement selects or fetch the record in the sign up table

SELECT Syntax

SELECT * FROM tablename;

Example

SELECT * FROM SIgnUP;

Output

We get the all the records from the tables

ID FN LN MobNo EmailId City

1 Rahul Gandhi 1111 [email protected] Pune

2 Arvind Kejriwal 2222 [email protected] Mumbai

3 Anna Hajare 3333 [email protected] Pune

4 Sharad Pawar 4444 [email protected] Baramati

5 Soniya Gandhi 5555 [email protected] Pune

* - rows & column select

Select particular column in the table

For EX. select FN & LN column

Syntax

SELECT column1, column2….n FROM tablename;

or

SELECT column1, column2….N

FROM tablename;
Example-

SELECT FN, LN FROM SignUP;

Or

SELECT FN, LN

FROM SignUP;

Output-

FN LN

1 Rahul Gandhi

2 Arvind Kejriwal

3 Anna Hajare

4 Sharad Pawar

5 Soniya Gandhi

2. Distinct
- Keyword / statement is used to return only distinct value/ different value/ unique value in
the particular column
- Table – column- many duplicate value – sometimes we want only unique value
- Ex. LN – distinct value select

Syntax

SELECT DISTINCT column 1, 2, 3….n

FROM tablename;

Example

SELECT DISTINCT LN FROM SignUp;

Output
LN

1 Gandhi

2 Kejriwal

3 Hajare

4 Pawar

Count

Syntax

SELECT COUNT (DISTINCT column 1, 2, 3….n)

FROM tablename;

Example

SELECT COUNT (DISTINCT LN) FROM SignUp;

Output

Number of records- 4

Without Distinct

SELECT coumnname 1, 2…n FROM tablename;

Example

SELECT LN FROM SignUp;

Output

LN

1 Gandhi

2 Kejriwal

3 Hajare
4 Pawar

5 Gandhi

TOP (SQL server)

SQL statement it used to display the top records in a table

Syntax

SELECT TOP value/number/percentage * FROM tablename;

Example

SELECT TOP 3 * FROM SignUp;

SELECT TOP 50% * FROM SignUp;

Output

ID FN LN MobNo EmailId City

1 1 Rahul Gandhi 1111 [email protected] Pune

2 2 Arvind Kejriwal 2222 [email protected] Mumbai

3 3 Anna Hajare 3333 [email protected] Pune

RONUM(Oracal)

SELECT * FROM tablename

Where RONUM<=value;

SELECT * FROM SignUp

Where RONUM<=3;

ID FN LN MobNo EmailId City

1 1 Rahul Gandhi 1111 [email protected] Pune

2 2 Arvind Kejriwal 2222 [email protected] Mumbai


3 3 Anna Hajare 3333 [email protected] Pune

Limit (MySql)

SELECT * FROM tablename

LIMIT value;

SELECT * FROM SignUp

LIMIT 2;

Output

ID FN LN MobNo EmailId City

1 1 Rahul Gandhi 1111 [email protected] Pune

2 2 Arvind Kejriwal 2222 [email protected] Mumbai

ORDER BY

Order by keyword – it used to sort the result set in ascending & descending order

It is used to display the records by ascending & descending order from selected column

Default- Ascending order

DESC- Descending order

Ascending order

SELECT * FROM tablename

ORDERBY column 1,2,3….n;


SELECT * FROM SignUp

ORDERBY FN;

ID FN LN MobNo EmailId City

1 3 Anna Hajare 3333 [email protected] Pune

2 2 Arvind Kejriwal 2222 [email protected] Mumbai

3 1 Rahul Gandhi 1111 [email protected] Pune

4 4 Sharad Pawar 4444 [email protected] Baramati

5 5 Soniya Gandhi 5555 [email protected] Pune

Descending order

SELECT * FROM tablename

ORDERBY column name DESC;

SELECT * FROM SignUp

ORDERBY FN DESC;

ID FN LN MobNo EmailId City

2 5 Soniya Gandhi 5555 [email protected] Pune

1 4 Sharad Pawar 4444 [email protected] Baramati

3 1 Rahul Gandhi 1111 [email protected] Pune

4 2 Arvind Kejriwal 2222 [email protected] Mumbai

5 3 Anna Hajare 3333 [email protected] Pune


Where clause

- Where clause is used to filter records


- It is used to extract only those records that fulfill a specified condition
- EX. Sign Up- City- Pune

SELECT * FROM tablename

WHERE condition;

CIndition= (Column name = value)

SELECT * FROM SignUp

WHERE City=Pune;

ID FN LN MobNo EmailId City

1 1 Rahul Gandhi 1111 [email protected] Pune

2 3 Anna Hajare 3333 [email protected] Pune

3 5 Soniya Gandhi 5555 [email protected] Pune

SELECT * FROM tablename

WHERE Columnane=value;

SELECT * FROM SignUp

WHERE ID=1;

ID FN LN MobNo EmailId City


1 1 Rahul Gandhi 1111 [email protected] Pune

SELECT coulmn1 ,2,3…FROM tablename

WHERE condition;

SELECT email id FROM SignUp

WHERE City=Pune;

Email Id
1 [email protected]
2 [email protected]
3 [email protected]

Type of Where Clause

1. Or
2. And

Logic gates

OR- 1 1 =1 1 0 =1 0 1 =1 0 0 =0

AND- 1 1 =1 1 0 =0 0 1 =0 0 0 =0

OR-

Use to select records when both condition or one condition is true

SELECT * FROM tablename

WHERE condition 1 OR condition 2;


SELECT * FROM SignUP

WHERE FN=’Rahul’ OR LN=’Gandhi’;

ID FN LN Mob. No Email Id City

1 1 Rahul Gandhi 1111 [email protected] Pune

2 5 Soniya Gandhi 5555 [email protected] Pune

SELECT column name 1,2, FROM tablename

WHERE condition 1 OR condition 2;

SELECT emaild FROM SignUp

WHERE FN=Rahul OR LN= Gandhi;

Email Id
1 [email protected]
2 [email protected]

AND

- Select records when both the condition must be true the we get the output will be true

SELECT * FROM tablename

WHERE condition 1 AND condition 2;

SELECT * FROM SignUp


WHERE FN=Rahul AND LN=Gandhi;

ID FN LN Mob. No Email Id City

1 1 Rahul Gandhi 1111 [email protected] Pune


SELECT column 1, 2 FROM tablename

WHERE condition 1 AND condition 2;

SELECT Mob.No FROM SignUp

WHERE FN=Rahul AND LN=Gandhi;

Mob. No

1 1111

Like Operator

Start name S/B/

End Name H

Like operator it is used in a where clause to search for a specified pattern in a column

1. % -----multiple characters
2. _ ------ single character

For starting with specific alphabets

SELECT * FROM tablename

WHERE columnname LIKE pattern;

SELECT * FROM SignUp

WHERE FN LIKE ‘A%’;


ID FN LN Mob. No Email Id City

1 2 Arvind Kejriwal 2222 [email protected] Mumbai

2 3 Anna Hajare 3333 [email protected] Pune

SELECT columnname FROM tablename

WHERE columnname LIKE pattern;

SELECT LN FROM SignUp

WHERE FN LIKE ‘A%’;

LN

1 Kejriwal

2 Hajare

For ending with specific alphabets

SELECT * FROM tablename

WHERE columnname LIKE pattern;

SELECT * FROM SignUp

WHERE FN LIKE ‘%d’;

ID FN LN Mob. No Email Id City

1 2 Arvind Kejriwal 2222 [email protected] Mumbai

2 4 Sharad Pawar 4444 [email protected] Baramati


SELECT columnname FROM tablename

WHERE columnname LIKE pattern;

SELECT LN FROM SignUp

WHERE FN LIKE ‘%d’;

LN

1 Kejriwal

2 Pawar

A%d

[ABC]%

%[ABC]

SELECT * FROM tablename

WHERE columnname NOT LIKE pattern;

SELECT * FROM SignUp

WHERE FN NOT LIKE ‘A%’;

ID FN LN Mob. No Email Id City

1 1 Rahul Gandhi 1111 [email protected] Pune

2 4 Sharad Pawar 4444 [email protected] Baramati


3 5 Soniya Gandhi 5555 [email protected] Pune

Select all records where the value of the column starts with the letter "A".

Syntax

SELECT * FROM table_name
WHERE column LIKE “A%”;

Select all records where the value of the column ends with the letter "a".

Syntax

SELECT * FROM table_name
WHERE column LIKE “%a”;

Select all records where the value of the column starts with letter "A" and ends with the
letter "b".

Syntax

SELECT * FROM table_name
WHERE column LIKE “A%b”;

Select all records where the value of the column contains the letter "a".

Syntax

SELECT * FROM table_name
WHERE column LIKE “%a%”;

Select all records where the value of the column does NOT start with the letter "A".
Syntax

SELECT * FROM table_name
WHERE column NOT LIKE “A%”;

For Starting with alphabets ABC

The following SQL statement selects all Sign Up with a FN starting with "A", "B", or "C":

Syntax

SELECT * FROM table_name
WHERE column LIKE “[ABC]%”;

For Ending with alphabets ABC

The following SQL statement selects all Sign Up with a FN ending with "A", "B", or "C":

Syntax

SELECT * FROM table_name
WHERE column LIKE “%[ABC]”;

LIKE Operator Description


WHERE Column LIKE 'a%' Finds any values that start with "a"
WHERE Column LIKE '%a'
Finds any values that end with "a"

WHERE Column LIKE '%or%' Finds any values that have "or" in any position
WHERE Column LIKE '_r%'
Finds any values that have "r" in the second
position

WHERE Column LIKE 'a_%'


Finds any values that start with "a" and are at
least 2 characters in length

WHERE Column LIKE 'a__%' Finds any values that start with "a" and are at
least 3 characters in length
WHERE Column LIKE 'a%o' Finds any values that start with "a" and ends
with "o"

Wildcard operator

- Used to substitute one or more characters in a string


- Like operator –wildcard
- Like operator – where clause to search specified pattern

SELECT * FROM tablename


WHERE colunmname Like specified pattern;

SELECT * FROM SignUp


WHERE FN LIKE ‘_ahul’;

ID FN LN Mob. No Email Id City

1 1 Rahul Gandhi 1111 [email protected] Pune

IN operator
- Used to fetch one or more data from the table
- It is use to select those records which specify in query

SELECT * FROM tablename

WHERE Columnname IN (‘Value1’,’value2’,’value3’….);

SELECT * FROM SignUp

WHERE FN IN (‘Rahul’,’Soniya’);

ID FN LN Mob. No Email Id City

1 1 Rahul Gandhi 1111 [email protected] Pune

2 5 Soniya Gandhi 5555 [email protected] Pune

SELECT * FROM tablename

WHERE Columnname NOT IN (‘Value1’,’value2’,’value3’….);

SELECT * FROM SignUp

WHERE FN NOT IN (‘Rahul’,’Soniya’);

ID FN LN Mob. No Email Id City

1 2 Arvind Kejriwal 2222 [email protected] Mumbai

2 3 Anna Hajare 3333 [email protected] Pune

3 4 Sharad Pawar 4444 [email protected] Baramati


Between Operator

- Selects values within a given range


- Value- number, text, date
- Include- begin & end value are included
- It is use to select value between ranges which specified

SELECT * FROM tablename

WHERE coumnname BETWEEN value1 AND value2;

SELECT * FROM SignUp

WHERE ID BETWEEN 2 AND 4;

ID FN LN Mob. No Email Id City

1 2 Arvind Kejriwal 2222 [email protected] Mumbai

2 3 Anna Hajare 3333 [email protected] Pune

3 4 Sharad Pawar 4444 [email protected] Baramati

SELECT * FROM SignUp

WHERE EmailId BETWEEN ‘[email protected]’ AND ‘[email protected]’;

ID FN LN Mob. No Email Id City

1 Rahul Gandhi 1111 [email protected] Pune

2 Arvind Kejriwal 2222 [email protected] Mumbai

3 Anna Hajare 3333 [email protected] Pune

NOT between
SELECT * FROM SignUp

WHERE ID NOT BETWEEN 2 AND 4;

ID FN LN Mob. No Email Id City

1 1 Rahul Gandhi 1111 [email protected] Pune

2 5 Soniya Gandhi 5555 [email protected] Pune

INSERT INTO

- INSERT INTO statement is used to insert new records in a table

INSERT INTO tablename

VALUES (value1, value2, value3…..)

INSERT INTO SignUp

VALUES (6, “Jayant”, “Patil”, 6666, [email protected], “Sangli”);

ID FN LN Mob. No Email Id City

1 1 Rahul Gandhi 1111 [email protected] Pune

2 2 Arvind Kejriwal 2222 [email protected] Mumbai

3 3 Anna Hajare 3333 [email protected] Pune

4 4 Sharad Pawar 4444 [email protected] Baramati

5 5 Soniya Gandhi 5555 [email protected] Pune

6 6 Jayant Patil 6666 [email protected] Sangli


Only insert data in specific column

INSERT INTO tablename (column1, coulmn2,coulmn3…)

VALUES (value1, value2, value3…)

INSERT INTO SignUp (FN,LN,Mob.No)

VALUES (“Amit”, “Deshmukh”, 7777);

ID FN LN Mob. No Email Id City

1 1 Rahul Gandhi 1111 [email protected] Pune

2 2 Arvind Kejriwal 2222 [email protected] Mumbai

3 3 Anna Hajare 3333 [email protected] Pune

4 4 Sharad Pawar 4444 [email protected] Baramati

5 5 Soniya Gandhi 5555 [email protected] Pune

6 6 Jayant Patil 6666 [email protected] Sangli

7 Amit Deshmukh 7777

UPDATE

- It is used to modify the existing records in a table

Ex. FN=Rahul ----Mob.No=0000, Email [email protected], City=Delhi

UPDATE tablename

SET coulmn1=value1, coulmn2=value2….

WHERE codition;
UPDATE SignUp

SET Mob.No=0000, Email [email protected], City=Delhi

WHERE FN=Rahul;

ID FN LN Mob. No Email Id City

1 1 Rahul Gandhi 0000 [email protected] Delhi

2 2 Arvind Kejriwal 2222 [email protected] Mumbai

3 3 Anna Hajare 3333 [email protected] Pune

4 4 Sharad Pawar 4444 [email protected] Baramati

5 5 Soniya Gandhi 5555 [email protected] Pune

UPDATE SignUp

SET Mob.No=0000, Email [email protected], City=Delhi

ID FN LN Mob. No Email Id City

1 1 Rahul Gandhi 0000 [email protected] Delhi

2 2 Arvind Kejriwal 0000 [email protected] Delhi

3 3 Anna Hajare 0000 [email protected] Delhi

4 4 Sharad Pawar 0000 [email protected] Delhi

5 5 Soniya Gandhi 0000 [email protected] Delhi

Condition= Coumnname=value

Delete

- It is used to delete existing records in table


- Used the particular row in to the table
- Ex. delete Mob.No=2222 from the SignUp table

DELETE FROM tablename

WHERE condition;

DELETE FROM SignUp

WHERE Mob.No=2222;

ID FN LN Mob. No Email Id City

1 Rahul Gandhi 1111 [email protected] Delhi

3 Anna Hajare 3333 [email protected] Pune

4 Sharad Pawar 4444 [email protected] Baramati

5 Soniya Gandhi 5555 [email protected] Pune

DELETE FROM SignUp

ID FN LN Mob. No Email Id City

SELECT INTO

- Select statement it is used to copies data from one table into a new table

SELECT * INTO newtablename FROM oldtablename;

SELECT*INTO Register FROM SignUp;


Tablename- Register

ID FN LN Mob. No Email Id City

1 Rahul Gandhi 1111 [email protected] Pune

2 Arvind Kejriwal 2222 [email protected] Mumbai

3 Anna Hajare 3333 [email protected] Pune

4 Sharad Pawar 4444 [email protected] Baramati

5 Soniya Gandhi 5555 [email protected] Pune

ALIAS

SQL statement it is used for to change the temporary column name & table name without
changing the database

SELECT coumnname AS aliasname

FROM tablename;

SELECT FN AS Firstname, LN AS Lastname

FROM SignUp;

ID Firstname Lirstname Mob. No Email Id City

1 Rahul Gandhi 1111 [email protected] Pune

2 Arvind Kejriwal 2222 [email protected] Mumbai

3 Anna Hajare 3333 [email protected] Pune

4 Sharad Pawar 4444 [email protected] Baramati

5 Soniya Gandhi 5555 [email protected] Pune


SELECT coumnname AS aliasname

FROM tablename AS aliastable;

SELECT FN AS Firstname, LN AS Lastname

FROM SignUp AS Register;

Tablename- Register

ID Firstname Lastname Mob. No Email Id City

1 Rahul Gandhi 1111 [email protected] Pune

2 Arvind Kejriwal 2222 [email protected] Mumbai

3 Anna Hajare 3333 [email protected] Pune

4 Sharad Pawar 4444 [email protected] Baramati

5 Soniya Gandhi 5555 [email protected] Pune

UNION & UNION ALL

- It is used to combine the result set of two or more select statement


- Union it is select only distinct(unique value) by default
- Same number of column
- Same data type
- Same order

SELECT columnname FROM table1

UNION

SELECT columnname FROM table2;

SELECT LN FROM SignUp

UNION
SELECT LN FROM Register;

SignUp

ID FN LN Mob. No Email Id City Seats

1 Rahul Gandhi 1111 [email protected] Pune 45

2 Arvind Kejriwal 2222 [email protected] Mumbai 49

3 Anna Hajare 3333 [email protected] Pune 45

4 Sharad Pawar 4444 [email protected] Baramati 55

5 Soniya Gandhi 5555 [email protected] Pune 49

Register

ID FN LN Mob. No Email Id City Seats

1 Rahul Gandhi 1111 [email protected] Pune 45

2 Arvind Kejriwal 2222 [email protected] Mumbai 49

3 Anna Hajare 3333 [email protected] Pune 45

4 Sharad Pawar 4444 [email protected] Baramati 55

5 Soniya Gandhi 5555 [email protected] Pune 49

6 Sanjay Raut 66666 [email protected] Mumbai 53

7 Uddhav Thakre 7777 [email protected] Mumbai 54

8 Rohit Pawar 8888 [email protected] Baramati 56

9 Dhiraj Deshmukh 0000 [email protected] Latur 57

LN

1 Deshmukh

2 Gandhi

3 Hajare

4 Kejriwal
5 Pawar

6 Raut

7 Thakre

SELECT Seats FROM SignUp

UNION

SELECT Seats FROM Register;

Seats

1 45

2 49

3 53

4 54

5 55

6 56

7 57

UNION ALL

- To allow duplicate values when use union all

SELECT LN FROM SignUp

UNION ALL

SELECT LN FROM Register;

Output- 14
Create Table

- To create a new table in a database

Syntax

CREATE TABLE tablename

Column1 datatype,

Column1 datatype,

Column1 datatype,

);

Column parameter / attributes specify the names of the column of the table

Data type – specify the type of data the column can hold

Example- to create table – SignUp

CREATE TABLE SignUp

ID int,

FN varchar(255)

LN varchar(255)

Mob.No int,

EmailId varchar(255)

City varchar(255)

)
Output- Structure

Tablename- SignUp

ID FN LN Mob. No Email Id City

Drop

- Drop table statement is used to drop an existing table in database


- Delete both structure and records in the table

DROP TABLE tablename;

DROP TABLE SignUp;

Output- blank/ delete both records & structure in the table

Truncate

- Truncate table statement is used to delete the all the records or delete the data inside the
table
- Not delete the structure of the table
- Only delete the all the records

TRUNCATE TABLE tablename;

TRUNCATE TABLE SignUp;

ID FN LN Mob. No Email Id City


Difference between Delete & Truncate

Delete Truncate
DML command DDL command
We can use where clause We can not use where clause
Slower than truncate Faster than delete statement
Delete a row from table Remove all the rows
Can rollback data after using delete statement Not possible to rollback after using the truncate
statement

Alter

- Alter table statement is used to add, delete , modify column in an existing table

Alter table – add column

ALTER TABLE tablename

ADD columnname datatype;

ALTER TABLE SignUp

ADD address varchar(255);

ID FN LN Mob. No Email Id City Address

1 1 Rahul Gandhi 1111 [email protected] Pune

2 2 Arvind Kejriwal 2222 [email protected] Mumbai

3 3 Anna Hajare 3333 [email protected] Pune

4 4 Sharad Pawar 4444 [email protected] Baramati

5 5 Soniya Gandhi 5555 [email protected] Pune

Alter table – drop column


- To delete a column in a table

ALTER TABLE tablename

DROP COLUMN coumnname;

ALTER TABLE SignUp

DROP address;

ID FN LN Mob. No Email Id City

1 1 Rahul Gandhi 1111 [email protected] Pune

2 2 Arvind Kejriwal 2222 [email protected] Mumbai

3 3 Anna Hajare 3333 [email protected] Pune

4 4 Sharad Pawar 4444 [email protected] Baramati

5 5 Soniya Gandhi 5555 [email protected] Pune

Altertable- Alter / modify column

- To change the datatype of a column in a table

SQL server/MS access

ALTER TABLE tablename

ALTER COLUMN coumnname datatype;

MY SQL / Oracal

ALTER TABLE tablename

MODIFY COLUMN columnname datatype;

ALTER TABLE SignUp

MODIFY COLUMN Mob.No decimal;


Output – mobile number the datatype is decimal

Null, not null, default (puneVCTC), check (Age<22), primary key (ID), Unique key (ID,EmailId,
mob.no), forgain key (two link)

I have featch your all data – another table – primary key – another table – id- 1

Constraints

- Sql constraints are used to specify rules for the data in table
- Constraints are used to limit the type of data that can go in to a table
- Constraints – column apply or table

CREATE TABEL tablename

CN1 datatype constraints,

CN1 datatype constraints,

CN1 datatype constraints

);

There are different types of constraints

1. Null
2. Not null
3. Unique key
4. Primary key
5. Forgien key
6. Default
7. Check
1. Not null
- Column can not have null value

Ex.

CREATE TABEL tablename

CN1 datatype NOTNULL,

CN1 datatype NOTNULL,

CN1 datatype NOTNULL

);

Ex.

CREATE TABLE SignUp

ID int NOTNULL,

FN varchar NOTNULL,

LN varchar NOTNULL

Mob.No int NOTNULL,

EmailId varchar (255) NOTNULL,

City varchar(255) NOTNULL,

2. Unique key
- To ensure all the values in a column are different
- Unique constraints allow only one NULL value
- Each table have more that one/multiple unique key / value / constraints
CREATE TABLE SignUp

ID int NOTNULL/ UNIQUE,

FN varchar NOTNULL,

LN varchar NOTNULL

Mob.No int NOTNULL / UNIQUE,

EmailId varchar (255) NOTNULL/ UNIQUE,

City varchar(255) NOTNULL,

Primary Key

- Primary key constraints uniquely identifies each records in a table


- Primary key constrains not allow null values
- Primary key – only one in a table

CREATE TABLE SignUp

ID int NOTNULL/ UNIQUE,

FN varchar NOTNULL,

LN varchar NOTNULL

Mob.No int NOTNULL / UNIQUE,

EmailId varchar (255) NOTNULL/ UNIQUE,

City varchar(255) NOTNULL,

PRIMARY KEY (ID)


)

Default

- Sets a default values for a column if no value is specified

City – all city pune

CREATE TABLE SignUp

ID int NOTNULL/ UNIQUE,

FN varchar NOTNULL,

LN varchar NOTNULL

Mob.No int NOTNULL / UNIQUE,

EmailId varchar (255) NOTNULL/ UNIQUE,

City varchar(255) NOTNULL default,

PRIMARY KEY (ID)

ID LN FN Age CITY

21 Pune

22 Pune

23 Pune

Check

- Ensure the value in a column satisfies a specific condition


- It is used to limit the value range that can be placed in a column
- It will allow only certain value for this column
CREATE TABLE SignUp

ID int NOTNULL/ UNIQUE,

FN varchar NOTNULL,

LN varchar NOTNULL

Mob.No int NOTNULL / UNIQUE,

EmailId varchar (255) NOTNULL/ UNIQUE,

City varchar(255) NOTNULL default,

PRIMARY KEY (ID)

CHECK(Age) > 21

CHECK(Age) < , > = value

FOREIGN KEY

- Use to link two tables together


- Accept multiple null value
- More than one foreign key in table
- FK is a filed in one table, that refers to the PK in another table
- FK- child table
- PK- parent table
CREATE TABLE SignUp

ID int NOTNULL/ UNIQUE,

FN varchar NOTNULL,

LN varchar NOTNULL

Mob.No int NOTNULL / UNIQUE,

EmailId varchar (255) NOTNULL/ UNIQUE,

City varchar(255) NOTNULL default,

FOREIGN KEY (ID)

Example

CREATE TABLE UIDAI

AADHAR NO int(25),

FIRSTNAME varchar(256) NOT NULL,

LASTNAME varchar(256) NOT NULL,

MOBILE int(20) UNIQUE,

EMAIL ID varchar(256) UNIQUE,

PAN CARD varchar(25) UNIQUE,

CITY varchar(25),
PRIMARY KEY (AADHAR NO)

CREATE TABLE ICICI

FIRSTNAME varchar(256),

LASTNAME varchar(256),

BALANCE int (20),

ACCOUNT NO int (20),

AADHAR NO int (20),

FOREIGN KEY (AADHAR NO) REFERENCE UIDAI (AADHAR NO)

CREATE TABLE IDBI

AADHAR NO. int (20),

BALANCE int (20),

FOREIGN KEY (AADHAR NO) REFERENCE UIDAI (AADHAR NO)

Notice that the "AADHAR NO" column in the "ICICI", "AADHAR NO" column in the "IDBI"
table points to the "AADHAR NO" column in the "UIDAI" table.

The "AADHAR NO" column in the "UIDAI" table is the PRIMARY KEY in the "UIDAI" table.
The "AADHAR NO" column in the "ICICI" table is a FOREIGN KEY in the "ICICI" table.

The "AADHAR NO" column in the "IDBI" table is a FOREIGN KEY in the "IDBI" table.

Difference between Primary key, Foreign key & Unique Key

Primary Key Foreign Key Unique Key


The PRIMARY KEY A FOREIGN KEY is a key used to link The UNIQUE
constraint uniquely identifies two tables together. A FOREIGN constraint ensures that
each record in a table. KEY is a field (or collection of fields) all values in a
Primary keys must contain in one table that refers to the column are different.
UNIQUE values PRIMARY KEY in another table.
Primary key cannot have a Foreign key can accept multiple null Unique Constraint
NULL value. value. may have a NULL
value.
Each table can have only one We can have more than one foreign Each table can have
primary key. key in a table. more than one
Unique Constraint.

JOIN-

Join is the sql statement it is used to join the two or more table based on the basic of relationship
between the primary key & forging key

There are 4 types of join

1. Inner join
2. Left join
3. Right join
4. Outer join

Inner join
- Keyword select records that have matching values in both tables
- It is use to show records in column which are related to common values

Syntax

SELECT column name (s)

FROM table 1 INNER JOIN table 2

ON table 1. Columnname = table 2 . columnname;

Or

ON table 1. PK= table 2 . FK;

Or

SELECT table1.coulmn1,

Table 1. column2,

Table 2. column1…….

FROM table 1 INNER JOIN table 2

ON table1.PK=table2.FK

Table1- UIDAI
Aadhar (PK) First Name Last Name City
1 Sharad Pawar Baramati
2 Ajit Pawar Pune
3 Sanjay Raut Mumbai
Table2- IDBI
Account No Balance Aadhar(FK)
1234 50000 2
5898 40000 3
9872 30000 2
8796 90000 5

Ex

SELECT UIDAI.First Name,

UIDAI. Last Name,

IDBI. Balance

FROM UIDAI INNER JOIN IDBI

ON UIDAI.Aadhar=IDBI.Aadhar;

Frist Name Last Name Balance


Ajit Pawar 50000
Ajit Pawar 30000
Sanjay Raut 40000

Left Join

- Keyword – returns all records from the left table & the matching records from the right
table
SELECT table1.coulmn1,

Table 1. column2,

Table 2. column1…….

FROM table 1 LEFT JOIN table 2

ON table1.PK=table2.FK

SELECT UIDAI.First Name,

UIDAI. Last Name,

IDBI. Balance

FROM UIDAI LEFT JOIN IDBI

ON UIDAI.Aadhar=IDBI.Aadhar;

First Name Last Name Balance


Sharad Pawar Blank
Ajit Pawar 50000
Sanjay Raut 40000
Ajit Pawar 30000

Right Join

- Return all records from the right table(table2) & the matching records from the left table
SELECT table1.coulmn1,

Table 1. column2,

Table 2. column1…….

FROM table 1 RIGHT JOIN table 2

ON table1.PK=table2.FK

SELECT UIDAI.First Name,

UIDAI. Last Name,

IDBI. Balance

FROM UIDAI RIGHT JOIN IDBI

ON UIDAI.Aadhar=IDBI.Aadhar;

First Name Last Name Balance


Ajit Pawar 50000
Ajit Pawar 40000
Sanjay Raut 30000
90000

Outer Join

It is a full outer join keyword – returns all records when there is a left and right table records
SELECT table1.coulmn1,

Table 1. column2,

Table 2. column1…….

FROM table 1 FULL JOIN table 2

ON table1.PK=table2.FK

SELECT UIDAI.First Name,

UIDAI. Last Name,

IDBI. Balance

FROM UIDAI FULL JOIN IDBI

ON UIDAI.Aadhar=IDBI.Aadhar;

First Name Last Name Balance


Sharad Pawar blank
Ajit Pawar 50000
Sanajy Raut 40000
Ajit Pawar 30000
90000

Aggregate function

- It returns the single value based on the particular column

Table-Students Marks
Sr.No FN LN Marks
1 Virat Kohli 65
2 Rohit Sharma 75
3 Ravindra Jadeja 95
4 Shikhar Dhawan 85
5 Rishabh Pant 55

1. First
2. Last
3. Max
4. Min
5. Sum
6. Avg
7. Count

First

- Return first value of the numeric column

SELECT FIRST(columnname)

FROM tablenmae;

SELECT FIRST(Marks)

FROM Students Marks;

FIRST(Marks)
1 65

LAST

- Return last value in the selected numeric column

SELECT LAST(columnname)

FROM tablenmae;

SELECT LAST(Marks)
FROM Students Marks;

LAST(Marks)
1 55

MIN

- Return minimum value in the selected numeric column

SELECT MIN(columnname)

FROM tablenmae;

SELECT MIN(Marks)

FROM Students Marks;

MIN(Marks)
1 55

MAX

- Will return maximum value in the selected numeric column

SELECT MAX(columnname)

FROM tablenmae;

SELECT MAX(Marks)

FROM Students Marks;

MAX(Marks)
1 95
SUM

- It return the total sum of the numeric column

SELECT SUM(columnname)

FROM tablenmae;

SELECT SUM(Marks)

FROM Students Marks;

SUM(Marks)
1 375

AVG

- Avg= sum/count
- It returns average value of the numeric column

SELECT AVG(columnname)

FROM tablenmae;

SELECTAVG(Marks)

FROM Students Marks;

AVG(Marks)
1 75

COUNT

- Return total number of records

SELECT COUNT(columnname)
FROM tablenmae;

SELECTCOUNT(Marks)

FROM Students Marks;

COUNT(Marks)
1 5
Group By

- Groups rows that have the same value into summary row
- Group by statement is often used with aggregate function

Table Name- ICICI

Account No Withdrawal
1 6000
2 7000
1 7000
3 6000
1 2000
2 4000
3 9000
4 3000

SELECT AGGREGATE FUNCTION (Columnname) FROM tablename

GORUP BY columnname;

SELECT SUM (Withdrawal) FROM ICICI

GROUP BY Account No;


Withdrawal
15000
11000
15000
3000

SELECT columnname AGGREGATE FUNCTION (Columnname) FROM tablename

GORUP BY columnname;

SELECT Account No SUM (Withdrawal) FROM ICICI

GROUP BY Account No;

Account No Withdrawal
1 15000
2 11000
3 15000
4 3000

Having

- It is a sql statement use to satisfy the given condition & it is use with the aggregate
function

SELECT AGGREGATE FUNCTION (Columnname) FROM tablename

GORUP BY columnname

HAVING AGGEGARTE FUNCTION (Coulnmane) > , < , = Value;

SELECT SUM (Withdrwal) FROM ICICI

GORUP BY Account No
HAVING SUM (Withdrwal) <=12000;

Withdrawal
11000
3000

SELECT column name AGGREGATE FUNCTION (Columnname) FROM tablename

GORUP BY columnname

HAVING AGGEGARTE FUNCTION (Coulnmane) > , < , = Value;

SELECT account no SUM (Withdrwal) FROM ICICI

GORUP BY Account No

HAVING SUM (Withdrwal) <=12000;

Account No Withdrwarl
2 11000
4 3000

How find min & max or highest salary in the employ table?

Find the highest salary

Table Name – Employ Info


Id. No FN LN Salary
1 Virat Kohli 1000
2 Rohit Sharma 2000
3 Ravindra Jadeja 3000
4 Shikhar Dhawan 4000
5 Rishabh Pant 5000
6 Suryakumar Yadav 6000
7 Ishan Kishan 7000
8 Hardik Pandya 8000
9 Jasprit Bumrah 9000
10 Bhuvneshwar Kumar 10000

1. Find min & max salary in the employee info


2. 2nd max salary / how to get 2nd max salary

SELECT MAX(Columname)FROM tablename


WHERE columnname NOT IN (SELECT MAX(Columname)FROM tablename);

SELECT MAX(Salary)FROM employee info


WHERE salary NOT IN (SELECT MAX(Columname)FROM tablename);

- 2 nd max/highest salary in the table -9000

3. 2nd min salary

SELECT MIN(Columname)FROM tablename


WHERE columnname NOT IN (SELECT MIN(Columname)FROM tablename);

SELECT MIN(Salary)FROM employee info


WHERE salary NOT IN (SELECT MIN(Columname)FROM tablename);

2000 WHERE salary NOT IN(1000)


=2000- 2nd highest salary

4. 3rd, 4th , 5th , 6th, 7th,……….n


How to get nth highest salary

SELECT TOP 1 Columnname


FROM (SELECT TOP N Columnname FROM tablename
ORDER BY columnname DESC)
ORDER BY coulmname;

3rd highest salary – 8000

SELECT TOP 1 Salary


FROM (SELECT TOP 3 salary FROM employee info
ORDER BY Salary DESC)
ORDER BY Salary;
=8000- 3rd highest salary

SELECT TOP 3 salary FROM employee info


ORDER BY Salary DESC

SELECT TOP 1 Salary


FROM tablename
ORDER BY Salary;

10000 10000 8000


9000 9000 9000
8000 8000 10000
7000
-
1000
7th highest salry

SELECT TOP 1 Salary


FROM (SELECT TOP 7 salary FROM employee info
ORDER BY Salary DESC)
ORDER BY Salary;

SELECT TOP 1 Salary


FROM tablename
ORDER BY Salary;

SELECT TOP 1 Salary


FROM ICICI
ORDER BY Salary;

Output
1000 10000 4000 4000
9000 9000 5000
8000 8000 6000
7000 7000 7000
6000 6000 8000
5000 5000 9000
4000 4000 10000
3000
2000
1000

RONUM- Oracal

To show 7th highest salary / How to get the 7th highest salary

 Syntax

SELECT MIN (Column) FROM table_name

WHERE Column IN (SELECT TOP 7 Column FROM table_name

ORDER BY Column DESC);

 Example

SELECT MIN (Salary) FROM Employ Info

WHERE Salary IN (SELECT TOP 7 Salary FROM Employ Info

ORDER BY Salary DESC);

To show 7th min salary / How to get the 7th min salary

 Syntax

SELECT MAX (Column) FROM table_name

WHERE Column IN (SELECT TOP 7 Column FROM table_name

ORDER BY Column);
 Example

SELECT MAX (Salary) FROM Employ Info

WHERE Salary IN (SELECT TOP 7 Salary FROM Employ Info

ORDER BY Salary

SQL Questions

1. Explain DML and DDL?


2. How many Aggregate functions are available in SQL?
3. What is the difference in BETWEEN and IN condition operators?
4. What is the difference between the HAVING clause and WHERE clause?
5. What is the difference between DELETE, TRUNCATE & DROP?
6. What are different Clauses used in SQL?
7. What are different SQL constraints?
8. What is the difference between UNIQUE key, PRIMARY KEY & FORGIN KEY constraints?
9. What are different JOINS used in SQL?
10. How to write a query to show the details of a student from Students table whose name start
with K?
11. What is the syntax to add a record to a table?
12. What is the syntax of GROUP BY in SQL?
13. Define the SQL DELETE statement.
14. Write a SQL SELECT query that only returns each name only once from a table?
15. Write an SQL query to get the first maximum salary of an employee from a table named
employee_table.
16. Write an SQL query to get the second maximum salary of an employee from a table named
employee_table.
17. Write an SQL query to get the third maximum salary of an employee from a table named
employee_table.
18. Write an SQL query to fetch unique values from a table?
19. Write an SQL query to fetch data from table whose name start with Vipul & Krishna?
20 Write an SQL query to print details of the Workers whose SALARY lies between 100000 and
500000.
21. Write an SQL query to print details of the Workers who have joined in Feb’2014.
22. Write an SQL query to fetch the count of employees working in the department ‘Admin’.
23. Write an SQL query to fetch worker names with salaries >= 50000 and <= 100000.

You might also like