Assignment - 158 Talha Khalil (SQL Queries)
Assignment - 158 Talha Khalil (SQL Queries)
Talha Khalil 1
Table of Contents
Creating a Table...........................................................................................................................................3
Insert in table..............................................................................................................................................4
To Insert/Delete Column/s in existing table................................................................................................5
Deleting a table in database........................................................................................................................6
Show Columns without repeating rows.......................................................................................................7
Deleting row or rows from column..............................................................................................................8
Where Clause..............................................................................................................................................9
Order Clause..............................................................................................................................................10
Logical Operators.......................................................................................................................................11
And Operator:........................................................................................................................................11
In Operator............................................................................................................................................12
Between operator;................................................................................................................................12
Arithmetic Operators.................................................................................................................................13
+ Operator.............................................................................................................................................13
Sub Operator.........................................................................................................................................13
Multiply operator..................................................................................................................................14
Divide Operator.....................................................................................................................................14
Operator Precedence................................................................................................................................15
Modify Data type of an Existing Table.......................................................................................................15
Update statement.....................................................................................................................................16
Update one Column in a row.................................................................................................................16
Update several columns in a row..........................................................................................................16
Simple Join.................................................................................................................................................17
Equi join.................................................................................................................................................17
Non-Equi-Join........................................................................................................................................18
PRIMARY AND FOREIGN KEY.....................................................................................................................19
Primary Key...........................................................................................................................................19
Foreign key............................................................................................................................................19
Testing Primary key...............................................................................................................................20
Talha Khalil 2
Creating a Table
To Create a table,use query:
//Create table_name (Column1 name Data type, Column2 name data
type);//
Created a table named Directory1 with four fields (First Name, Last
Name, Number, Address)
Using the Query:
Create Table Directory1 (First_name char (50) ,
Last_name char (50) , Address Varchar (50) , Number
numeric (20));
To show Table use Query:
//Select * from student;//
Talha Khalil 3
Insert in table
To insert new rows in table we use query:
//Insert into table_name values (value1, value2, value3) ;//
Or we can insert in individual columns by using query:
// Insert into table_name (column1, column2, column3) values (value1,
value2, value3)//
Talha Khalil 4
To Insert/Delete Column/s in existing table
ALTER Statements:
The ALTER TABLE Statement is used to add or drop columns in an existing table.
It is also used to change the data type of an existing field of a table.
Using Query:
Alter table Directory1 Add remarks numeric;
To remove Remarks column we use querry:
Alter table Directory1 Drop column remarks;
Talha Khalil 5
Deleting a table in database
To delete a table from database we use command or query:
//Drop table table_name; //
Talha Khalil 6
Show Columns without repeating rows
Before using command;
Talha Khalil 7
Deleting row or rows from column
Delete statement:
Delete statement is used to delete row or rows in a table.
Syntax:
To delete a single row we use:
Delete from table-name where Column-name = row you want to delete;
To delete all rows:
Delete From Table-name;
As you can see all the rows are deleted from Directory1 After using
command:
Delete from Directory1;
Talha Khalil 8
Where Clause
Where clause is used to retrieve Data from table conditionally it can
appear only after from clause.
Syntax:
Select * from table-name where Column name condition;
We can use (,) between column names to select multiple columns if you
want to.
Talha Khalil 9
Order Clause
Order Clause is used to Sort the rows in ascending or descending order
using command:
Select * from table-name order by column-name ASC or DESC;
Or
Select * from table-name order by column-name;
Talha Khalil 10
Logical Operators
Operator Description
And Display if all conditions are true
Or Display if any condition is true
Not Negates an Expression or condition
In Display if any listed condition is true
Between Display if it Operand is in within range of comparison
And Operator:
In
Operator
Talha Khalil 11
Command used:
Select * from Directory1 where Networth IN
(1000,2000,10000);
Between operator;
Command used:
Select * from Directory1 where Networth between 1000
and 5000;
Arithmetic Operators
Operators Description
+ To Add
- To Sub
* To Multiply
/ To Divide
+ Operator
After using command:
Select Networth+1000 from Directory1;
Talha Khalil 12
Sub Operator
After using command:
Select First_name,Networth-1000 from Directory1;
Multiply operator
Query Used:
Select First_name,Networth*10 from Directory1;
Divide Operator
Query Used:
Select First_name,Networth/10 from Directory1;
Talha Khalil 13
Operator Precedence
The Order in which the different type of operators is evaluated is known
as an operator Precedence of operators it is also known as hierarchy of
operator’s multiplication and division is performed before addition and
subtraction.
Query Used:
Select First_name,Networth+300*12 from Directory1;
Talha Khalil 14
Update statement
The update statement is used to modify the existing data in a table.
Its syntax is:
Update table-name SET column name =new value WHERE Column
name=some value;
We can use (,) between column names to update multiple columns data.
Update one Column in a row
Query used:
update Directory1 set Networth=3000 Where
First_name='Subhan';
Talha Khalil 15
Simple Join
Simple join is the most common type of join.it retrieves rows from
tables. these table should have a common column or set of columns that
can be Logically related.
Equi join
A type of join that is based on Equalities is called equi-join.
Syntax:
Select table1.column,table2,column
From table1,table
Where table1.column=table2,column
Query Used;
Select
Directory1.Address,Directory1.First_name,Directory2.Address,
Directory2.Networth from Directory1,Directory2 Where
Directory1.Address=Directory2.Address;
Before Columns
After
Talha Khalil 16
Non-Equi-Join
A Non-Equi-join specifies the relation between columns of tables by
using (>,<,>=,<=,<>) other than =. The following example is illustrating
Non-Equi-Join.
Query Used
SelectDirectory1.Networth,Directory1.First_name,Directory2.First_name,
Directory2.Networth from Directory1,Directory2 Where
Directory2.Networth>Directory1.Networth;
Tables Before
Tables After
As we can see after using > in non equi
join greater net worth are organized
Talha Khalil 17
PRIMARY AND FOREIGN KEY
Primary Key
A Primary key is used to define a column or set of columns as primary key. It
restricts duplication of rows and does not allow null values.
Query I used to create a table with foreign key:
CREATE TABLE EMP(DEPTNO INT PRIMARY KEY , DNAME CHAR(50),
LOCATION CHAR(50));
Foreign key
Used to define a column or set of columns as foreign key. Foreign key is used to
create link between two tables by using Primary key of the parent table. The table
in which the foreign is used is known as Child
table
Using Query:
CREATE TABLE SALARY(DEPTNO INT FOREIGN
KEY REFERENCES EMP(DEPTNO),EMPID
NUMERIC(10),ENAME CHAR(20),SALARY MONEY);
Here DEPTNO is common in both table and act
as a Foreign key.
Talha Khalil 18
Testing Primary key
We can check working of Primary key by inserting data into Child table using the
Primary key in Parent table
Using Query:
INSERT SALARY VALUES (10 ,101,'TALHA',50000);
INSERT SALARY VALUES (20 ,102,'Subhan',60000);
INSERT SALARY VALUES (30 ,103,'Umar',70000);
Using Query
INSERT SALARY VALUES (40 ,103,'Umar',70000);
As we can see if we 40 as primary key then data cannot be entered so the primary
key is working properly.
Talha Khalil 19