Database Testing
Using SQL
Ahmed Elkholy
01551942560
1|P a g e
Database Testing: this type of testing is very useful if you have an UI
app that modify data and you want to test that UI app.
So we use SQL to make sure that modification of data is correct.
And instead of downloading a DBMS (Database Management System),
we will use an online DBMS from W3SCHOOLS .
And here is a reference if you want to go deeply in SQL queries.
“ -- ” used for one line comment in any SQL editor
Select
This query used to show data in the database , and asterisk ( * )
means all in view .
SELECT * FROM Customers ;
SELECT * FROM Customers LIMIT 3;
SELECT CustomerName , Address FROM Customers ;
SELECT LastName , Photo FROM Employee ;
Update
This query used to update data in database (already exist ) , if you
didn’t write WHERE all contact name and address will change into
Elkholy and Alex .
UPADTE Customers
SET ContactName = ‘Elkholy’ , Address =’Alex’
WHERE CustomerID =1 ;
2|P a g e
Insert
This query used to insert data into existing table
INSERT INTO Customers( CustomerName , ContactName , Address ,City ,PostalCode ,Country )
VALUES('Ahmed Elkholy' , 'Shawki' , 'Tra' , 'Damanhour ' , 22511 , 'Egypt' );
Select Statements
DISTINCT: used to view distinct or special data .
COUNT: view number of items or data .
IN : for char data , BETWEEN : for numeric data .
'%a' : word ends with a .
'a%' : word starts with a .
'%a%' : word contains a .
SELECT DISTINCT Country FROM Customers ;
SELECT COUNT(DISTINCT Country) FROM Customers ;
SELECT * FROM Customers
WHERE Country IN ('Germany' , 'UK');
SELECT * FROM Customers
WHERE Country NOT IN ('Germany' , 'UK');
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20 ;
SELECT * FROM Customers
WHERE CustomerName LIKE '%a%';
SELECT * FROM Customers
WHERE CustomerName iLIKE '%a%'; // !sensitiv ecase
3|P a g e
Order By
This query used to order data during view , DESC means from bigger
to smaller .
SELECT * FROM Customers
ORDER BY CustomerName ;
SELECT * FROM Products
ORDER BY Price DESC ;
SELECT * FROM Products
ORDER BY SupplierID , Price ;
SELECT * FROM Products
ORDER BY SupplierID DESC , Price ;
Group By
For example if you want to know how many customers from each
country or how many item for each price, Mostly Used with
Aggregate Functions .
SELECT COUNT (CustomerID), Country FROM Customers
GROUP BY Country ;
SELECT COUNT (ProductID) , Price FROM Products
GROUP BY Price ;
Aggregate functions
For example minimum , maximum , average and sum
SELECT AVG(Price)AS theAverage FROM Products ;
SELECT SUM(Price) FROM Products ;
SELECT MAX(Price) FROM Products ;
SELECT MIN(Price) FROM Products ;
4|P a g e
Creating a Database
To create a database
CREATE DATABASE Ecommerce ;
To delete a database
DROP DATABASE Ecommerce ;
Creating a Table
To create a table inside a Database, insert data into that table.
CREATE TABLE Persons (
ID INT ,
Name VARCHAR(255) ,
Phone INT
)
Add Data into Table
INSERT INTO Persons (ID ,Name ,Phone )
VALUES ('340' , 'Ahmed Elkholy' ,'01551942560' );
Modify Columns in Table
ALTER TABLE Persons
ADD Address VARCHAR(255);
ALTER TABLE Persons –- to delete a column
DROP COLUMN Address;
Update Data in Table
UPDATE Persons
SET Address ='Tra , Egypt '
WHERE ID = 18340 ;
Delete the Entire Table
DROP TABLE Persons;
5|P a g e
Inner join
To view data from two different Tables , but have a common
Columns
Note: Null Values will not be viewed.
SELECT Orders.OrderID , Customers.CustomerName
FROM Orders INNER JOIN Customers
ON Orders.CustomerID = Customers.CustomerID ;
6|P a g e