18 Dec 2021 - Database (SQL)
18 Dec 2021 - Database (SQL)
1. B
2. I
3. E
4. D/B
5. S
6. C
Database Testing
FN- M
LN- C
Email- CM
Mb. No-12
Address-Pune
Submit
Database-
What is SQL-
SQL- used
- Database creation
- deletion
- Fetching row
- Modifying row
- 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
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
- 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
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
SELECT Syntax
Example
Output
Syntax
or
FROM tablename;
Example-
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
FROM tablename;
Example
Output
LN
1 Gandhi
2 Kejriwal
3 Hajare
4 Pawar
Count
Syntax
FROM tablename;
Example
Output
Number of records- 4
Without Distinct
Example
Output
LN
1 Gandhi
2 Kejriwal
3 Hajare
4 Pawar
5 Gandhi
Syntax
Example
Output
RONUM(Oracal)
Where RONUM<=value;
Where RONUM<=3;
Limit (MySql)
LIMIT value;
LIMIT 2;
Output
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
Ascending order
ORDERBY FN;
Descending order
ORDERBY FN DESC;
WHERE condition;
WHERE City=Pune;
WHERE Columnane=value;
WHERE ID=1;
WHERE condition;
WHERE City=Pune;
Email Id
1 [email protected]
2 [email protected]
3 [email protected]
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-
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
Mob. No
1 1111
Like Operator
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
LN
1 Kejriwal
2 Hajare
LN
1 Kejriwal
2 Pawar
A%d
[ABC]%
%[ABC]
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%”;
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]%”;
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]”;
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 3 characters in length
WHERE Column LIKE 'a%o' Finds any values that start with "a" and ends
with "o"
Wildcard operator
IN operator
- Used to fetch one or more data from the table
- It is use to select those records which specify in query
WHERE FN IN (‘Rahul’,’Soniya’);
NOT between
SELECT * FROM SignUp
INSERT INTO
UPDATE
UPDATE tablename
WHERE codition;
UPDATE SignUp
WHERE FN=Rahul;
UPDATE SignUp
Condition= Coumnname=value
Delete
WHERE condition;
WHERE Mob.No=2222;
SELECT INTO
- Select statement it is used to copies data from one table into a new table
ALIAS
SQL statement it is used for to change the temporary column name & table name without
changing the database
FROM tablename;
FROM SignUp;
Tablename- Register
UNION
UNION
SELECT LN FROM Register;
SignUp
Register
LN
1 Deshmukh
2 Gandhi
3 Hajare
4 Kejriwal
5 Pawar
6 Raut
7 Thakre
UNION
Seats
1 45
2 49
3 53
4 54
5 55
6 56
7 57
UNION ALL
UNION ALL
Output- 14
Create Table
Syntax
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
ID int,
FN varchar(255)
LN varchar(255)
Mob.No int,
EmailId varchar(255)
City varchar(255)
)
Output- Structure
Tablename- SignUp
Drop
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
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
DROP address;
MY SQL / Oracal
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
);
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.
);
Ex.
ID int NOTNULL,
FN varchar NOTNULL,
LN varchar 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
FN varchar NOTNULL,
LN varchar NOTNULL
Primary Key
FN varchar NOTNULL,
LN varchar NOTNULL
Default
FN varchar NOTNULL,
LN varchar NOTNULL
ID LN FN Age CITY
21 Pune
22 Pune
23 Pune
Check
FN varchar NOTNULL,
LN varchar NOTNULL
CHECK(Age) > 21
FOREIGN KEY
FN varchar NOTNULL,
LN varchar NOTNULL
Example
AADHAR NO int(25),
CITY varchar(25),
PRIMARY KEY (AADHAR NO)
FIRSTNAME varchar(256),
LASTNAME varchar(256),
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.
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
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
Or
Or
SELECT table1.coulmn1,
Table 1. column2,
Table 2. column1…….
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
IDBI. Balance
ON UIDAI.Aadhar=IDBI.Aadhar;
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…….
ON table1.PK=table2.FK
IDBI. Balance
ON UIDAI.Aadhar=IDBI.Aadhar;
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…….
ON table1.PK=table2.FK
IDBI. Balance
ON UIDAI.Aadhar=IDBI.Aadhar;
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…….
ON table1.PK=table2.FK
IDBI. Balance
ON UIDAI.Aadhar=IDBI.Aadhar;
Aggregate function
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
SELECT FIRST(columnname)
FROM tablenmae;
SELECT FIRST(Marks)
FIRST(Marks)
1 65
LAST
SELECT LAST(columnname)
FROM tablenmae;
SELECT LAST(Marks)
FROM Students Marks;
LAST(Marks)
1 55
MIN
SELECT MIN(columnname)
FROM tablenmae;
SELECT MIN(Marks)
MIN(Marks)
1 55
MAX
SELECT MAX(columnname)
FROM tablenmae;
SELECT MAX(Marks)
MAX(Marks)
1 95
SUM
SELECT SUM(columnname)
FROM tablenmae;
SELECT SUM(Marks)
SUM(Marks)
1 375
AVG
- Avg= sum/count
- It returns average value of the numeric column
SELECT AVG(columnname)
FROM tablenmae;
SELECTAVG(Marks)
AVG(Marks)
1 75
COUNT
SELECT COUNT(columnname)
FROM tablenmae;
SELECTCOUNT(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
Account No Withdrawal
1 6000
2 7000
1 7000
3 6000
1 2000
2 4000
3 9000
4 3000
GORUP BY columnname;
GORUP BY columnname;
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
GORUP BY columnname
GORUP BY Account No
HAVING SUM (Withdrwal) <=12000;
Withdrawal
11000
3000
GORUP BY columnname
GORUP BY Account No
Account No Withdrwarl
2 11000
4 3000
How find min & max or highest salary in the employ table?
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
Example
To show 7th min salary / How to get the 7th min salary
Syntax
ORDER BY Column);
Example
ORDER BY Salary
SQL Questions