Todays Session Notes
Todays Session Notes
Till now we have seen how to test the application from the Front-End side(UI side) in
that we have covered Functional and Non Functional part.
Now will see how to test the data which were entered from UI side and that will be
stored in the Data Base.
Data Base-1
Server
Software/Application
Data Base-2
Database:
• A database is an organized collection of data, so that it can be easily accessed and
managed.
• The main purpose of the database is to operate a large amount of information by
storing, retrieving, and managing data.
• We can organize data into tables, rows, columns, and index it to make it easier to
find relevant information. While manipulating the data.
• So for accessing the data we need to understand special language which is known
as SQL that can be easily manipulate the data with the predefine set of syntax.
SQL:
• SQL stands for Structured Query Language.
• It is a database computer language designed for the retrieval and management of
data from the database. Also it is not a Case Sensitive Language.
• SQL is the standard language for Relational Database System. All the Relational
Database Management Systems (RDMS) like MySQL, MS Access, Oracle, Sybase,
Informix, Postgres and SQL Server use SQL as their standard database language.
e
SQL Commands:
SQL Commands
DDL changes the structure of the table like creating a table, deleting a table, altering a
table, etc.
All the command of DDL are auto-committed that means it permanently save all the
changes in the database.
Some commands that come under DDL:
• CREATE
• ALTER
• DROP
• TRUNCATE
The command of DML is not auto-committed that means it can't permanently save all
the changes in the database. They can be rollback.
Some commands that come under DML:
• INSERT
• UPDATE
• DELETE
Note: If we don’t provide the column name with its value then the value for that
column is set to NULL.
Task : Create your own database which consist of all the student information
from 12Feb 2022 morning batch.
3. DESCRIBE– it is a SQL statement used to describe the structure of the table.
SYNTAX- DESC <TABLE NAME>;
EX- DESC student;
4. SELECT- It is a SQL statement used to fetch a data, columns data/whole table data
from database/table.
SELECT * = Select all data from the table.
SYNTAX- SELECT *FROM <TABLE NAME>;
EX- SELECT * FROM Student;
7. SELECT COLUMN WITH (WHERE CLAUSE)- It is a SQL statement used to fetch data
of particular column data from the table along with it extract those records which
fulfil the desired condition.
SYNTAX-
SELECT <Column>FROM <TABLE NAME> WHERE <COLUMN NAME>= ‘DATA
VALUE’;
Ex- SELECT ID, FirstName FROM STUDENT WHERE FirstName = ‘Ashish’;
8. SELECT WITH (WHERE CLAUSE + AND)- It is a SQL statement used to fetch data of
particular data from the table along with it extract those records which fulfil the
desired condition (i.e AND Condition).
SYNTAX-
SELECT * FROM <TABLE NAME> WHERE <COLUMN NAME>= ‘DATA VALUE’ AND
<COLUMN NAME>= ‘DATA VALUE’;
SELECT ID, FirstName FROM STUDENT WHERE FirstName = ‘Test’ AND LastName =
‘Demo’;
9. SELECT COLUMN WITH (WHERE CLAUSE + OR)- It is a SQL statement used to fetch
data of particular data from the table along with it extract those records which
fulfil the desired condition. (i.e OR Condition).
SYNTAX-
SELECT * FROM <TABLE NAME> WHERE <COLUMN NAME>= ‘DATA VALUE’ OR
<COLUMN NAME>= ‘DATA VALUE’;
Ex-
SELECT * FROM STUDENT WHERE FirstName = ‘Test’ OR LastName = ‘Demo’;
10. DELETE – It is SQL statement used to delete all records from table. Excluding the
columns.
SYNTAX- DELETE FROM <TABLE NAME>;
EX- DELETE FROM Student;
11. DELETE WITH (WHERE CLAUSE )- – It is a SQL statement used to Delete the data
from the table which fulfil the desired condition.
13. DELETE WITH (WHERE CLAUSE + OR )- – It is a SQL statement used to Delete the
data from the table which fulfil the desired condition(i.e OR Condition).
14. TRUNCATE- It is a SQL statement used to deletes the data inside a table, but not
the table itself. (Column remains same/exist).
15. DROP TABLE– It is SQL statement used to delete structure of table with all
records.
16. ALTER- This command use to adds, deletes, or modifies columns in a table.
ALTER ADD- This command use to add the columns in a table.
SYNTAX-
ALTER TABLE <TABLE NAME>
ADD <COLUMN_NAME> <DATATYPE> <DATASIZE>;
EX-
ALTER TABLE STUDENT
ADD AGE VARCHAR2(30);
17. ALTER MODIFY- This command use to Modify the column Data Type from the
table.
SYNTAX-
ALTER TABLE <TABLE NAME>
MODIFY <COLUMN_NAME> <DATA TYPE> <SIZE>;
EX-
ALTER TABLE STUDENT
MODIFY RollNo VARCHAR2(20);
18. ALTER RENAME- This command use to RENAME the columns from the table.
SYNTAX-
ALTER TABLE <TABLE NAME>
RENAME COLUMN <COLUMN_NAME> TO <COLUMN_NAME>;
EX-
ALTER TABLE STUDENT
RENAME COLUMN RollNo TO STUDENT_ROLLNO;
19. UPDATE – It is a SQL statement used to update the data values of particular
record in the table.
• Sum() == This fun() calculate the sum of the values from the mentioned
column.
Syntax - SELECT SUM <COLUMN_NAME> FROM <TABLE_NAME>;
• Avg() == This fun()calculate the average value from the mentioned column.
Syntax- SELECT AVG <COLUMN_NAME> FROM <TABLE_NAME>;
• Min() == This fun() fetch the minimum value from the mentioned column.
Syntax - SELECT MIN <COLUMN_NAME> FROM <TABLE_NAME>;
• Max() == This fun() fetch the maximum value from the mentioned column.
Syntax- SELECT MAX <COLUMN_NAME> FROM <TABLE_NAME>;
ORDER BY
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
ORDER BY keyword sorts the records in ascending order by default. To sort the
records in descending order, use the DESC keyword.
SQL ALIAS
SQL aliases are used provide temporary name to give a table or a column in a table.
TIME SPAN - only exists for the duration of that query.
HOW TO USE – It is created with the AS keyword.
SYNTAX-
1. FOR COLUMN –
SELECT <COLUMN NAME> AS ALIAS_NAME FROM <TABLE NAME>
EX- SELECT SALARY AS EMP_SALARY FROM EMPLOYEE;
2. FOR TABLE-
SELECT <COLUMN NAME> FROM <TABLE NAME> AS ALIAS_NAME
EX- SELECT SALARY FROM EMPLOYEE AS ALIAS_NAME;
GROUP BY
GROUP BY clause is used with SELECT statement to arrange identical d ata into groups.
This GROUP BY clause follows the WHERE clause in a SELECT statement.
Syntax-
SELECT A.F. (*) FROM <TABLE NAME> GROUP BY <COLUMN NAME>
Que 1: FIND COUNT OF EMPLOYEE WHICH ARE WORKING IN THE SAME DEPARTMENT
ALONG WITH THE DEPARTMENT NAME? (EX- HR-2, Admin- 1, Dev -3 etc..)
O/P:
Que 2: Find MINIMUM SALARY FROM EACH DEPARTMENT?
O/P:
TASK - Que 7: Find MAXIMUM SALARY WITH EMPLOYEE COUNT FROM DEV
DEPARTMENT?
DISTINCT
DISTINCT statement is used to return only distinct (different) values. Which means it
hide repeated or duplicate records from table.
• NOT NULL
• UNIQUE
• PRIMARY KEY
• FOREIGN KEY
• CHECK - Ensures that the values in a column satisfies a specific condition
• DEFAULT - Sets a default value for a column if no value is specified
• CREATE INDEX - Used to create and retrieve data from the database very quickly
EX- CREATE TABLE TEMP_TABLE (ID INT NOT NULL, F_NAME VARCHAR2(10), L_NAME
VARCHAR2(10));
EX- CREATE TABLE TEMP_TABLE (ID INT UNIQUE, F_NAME VARCHAR2(10), L_NAME
VARCHAR2(10));
The table with the foreign key is called the child table, and the table with the primary
key is called the referenced or parent table.
*IMP*
Primary key cannot be null on the other hand foreign key can be null.
Primary key is always unique while foreign key can be duplicated.
SYNTAX-
CREATE TABLE <TABLE NAME> (COLUMN1 DATATYPE(DATASIZE), COLUMN2
DATATYPE(DATASIZE), COLUMN3 DATATYPE(DATASIZE), FOREIGN KEY(COLUMN1)
REFERENCES TABLENAME1 (COLUMN1));
EX-
PRIMARY KEY-
CREATE TABLE TEMP_TB(ID INT , FN VARCHAR2(20), LN VARCHAR2(20), PRIMARY KEY
(ID)):
FOREIGN KEY-
CREATE TABLE TEMP_TB1 (CO1 INT, CO2 INT, CO3 INT, FOREIGN KEY(CO1)
REFERENCES <TABLE_NAME>(ID));
TASK- FIND OUT THE DIFFERENCE BETWEEN PRIMARY KEY AND FOREIGN KEY.
SQL- JOINS
As the name shows, JOIN means to combine something. In case of SQL, JOIN means
"to combine two or more tables".
The SQL JOIN clause takes records from two or more tables in a database and
combines it together.
INNER JOIN: Inner Joins Combine Records From Two Tables Whenever There Are
Matching Values In A Field Co mmon To Both Tables.
SYNTAX: EX:
SYNTAX: EX:
SELECT * FROM <TABLE_1> SELECT * FROM S_I NFO
RIGHT JOIN <TABLE_2> RIGHT JOIN S_LOCATION
ON TABLE_1.ID = TABLE_2.ID; ON S_INFO.ID = S_LOCATION.ID;
LEFT JOIN: Left join returns all rows from the left table, and the matching rows from
the right table. The result is N ULL from the right side, if there is no match.
SYNTAX: EX:
SELECT * FROM <TABLE_1> SELECT * FROM S_INFO
LEFT JOIN <TABLE_2> LEFT JOIN S_LOCATION
ON TABLE_1.ID = TABLE_2.ID; ON S_INFO.ID = S_LOC ATION.ID;
FULL JOIN: Full join returns all records from right and left table. The result is NULL
from the both table, if there is no match.
SYNTAX: EX:
SELECT * FROM <TABLE_1> SELECT * FROM S_INFO
FULL JOIN <TABLE_2> FULL JOIN S_LOCATION
ON TABLE_1.ID = TABLE_2.ID; ON S_INFO.ID = S_LOCATION.ID;