Ch7 SQL (Basics)
Ch7 SQL (Basics)
(SQL)
1
UCS310-Database Management System
Introduction to SQL
What is SQL?
– SQL stands for Structured Query Language.
– SQL is used to create, remove, alter the database and database
objects in a database management system and to store,
retrieve, update the data in a database.
– SQL is a standard language for creating, accessing,
manipulating database management system.
– The program will go through all the records in the database file
and select those records that satisfy the condition (searching).
Command Description
Create to create new table or database
Alter for alteration
Truncate delete data from table
Drop to drop a table
Rename to rename a table
Command Description
Insert to insert a new row
Update to update existing row
Delete to delete a row
Merge merging two rows or two tables
Command Description
Commit to permanently save
Rollback to undo change
Savepoint to save temporarily
Command Description
Grant grant permission of right
Revoke take back permission.
The above query will delete all the records of students table.
The above query will delete the students table completely. It can also be used on
Databases. For Example, to drop a database,
Insert command is used to insert data into a table. Following is its general
syntax,
Suppose the age column of student table has default value of 18, then for the age field
18 value will ne added in new record.
The above command will delete all the records from Students table.
Example to Delete a particular Record from a Table
The above command will delete the record where id is 5 from Students table.
The table above contains six records and seven columns (CustomerID,
CustomerName, ContactName, Address, City, PostalCode, and Country).
City
Berlin
México D.F.
Luleå
London
Comparison
1. IN: The IN command allows you to specify multiple values in
a WHERE clause. The IN operator is a shorthand for multiple
OR conditions.
Syntax: Select col1 from tablename Where IN ( value1,
value2, value3);
2. Between: The BETWEEN command is used to select values
within a given range. The values can be numbers, text, or
dates.
Syntax: Select col1 from tablename Where condition
BETWEEN Value1 AND Value2;
3. Like: The LIKE command is used in a WHERE clause to
search for a specified pattern in a column.
Syntax: Select col1 from tablename Where col like ‘val1’;
Comparison (IN)
Note: We are using the Customer Table for all the examples.
eg 1. Extract all values from customer table of id 1, 4 and 5.
Query: Select * from Customer where id IN (1,4,5);
Output:
Comparison (Between)
Note: We are using the Customer Table for all the examples.
eg 3. Extract all values from customer table between 2 to 5.
Query: Select * from Customer where CustomerId Between 2 and 5;
Output:
Comparison (LIKE)
Note: We are using the Customer Table for all the examples.
eg 5. Extract all values from customer table with a
CustomerName starting with a.
Query: Select * from Customer where CustomerName LIKE ‘a
%’
Output:CustomerName
CustomerID Contact Address City PostalCod Country
Name e
1 Aman Sharma Obere Str. 57 Berlin 12209 Germany
A B
OUTPUT ID NAME
1 abhi
2 adam
3 chester
Union All
This operation is similar to Union. But it also shows the duplicate rows.
OUTPUT
ID NAME
1 abhi
2 adam
2 adam
3 chester
Intersection
Intersect operation is used to combine two SELECT statements, but it
only returns the records which are common from
both SELECT statements. In case of Intersect the number of columns
and datatype must be same.
1 abhi
More Examples
Customer Employee
first_name last_name first_name last_name
Stephen Jones Christina Jones
Mark Smith Michael McDonald
Denise King Paula Johnson
Paula Johnson Stephen Jones
Richard Archer Richard Smith
56
Union
Output:
First_name last_name
Stephen Jones
Mark Smith
Denise King
Paula Johnson
Richard Archer
Christina Jones
Michael McDonald
Richard Smith
FIRST_NAME LAST_NAME
Paula johnson
Stephen jones
FIRST_NAME LAST_NAME
Denise king
Mark Smith
Richard Archer
1A 1A
1A
COUNT( )
1B
1B
1B
1B 1B
COUNT( )
1B
1B
1C
1C 1C
1C
COUNT( )
Student
UCS310-Database Management System 64
Grouping
eg. 11 List the number of students of each class.
SELECT class, COUNT(*) FROM student
GROUP BY class;
Result
1A 1A
1A
AVG( )
1B
1B
1B
1B
1B
AVG( )
1B
1B
1C
1C 1C AVG( )
1C
Student
UCS310-Database Management System 67
Grouping
eg. 12 List the average Math test score of each class.
Result