Upload
athangaraj87
View
185
Download
5
Tags:
Embed Size (px)
Citation preview
Structured Query Language
– A query is a user request to retrieve data or information with a certain condition.
– SQL is a query language that allows user to specify the conditions.
– SQL works with database programs like MS Access, DB2, MS SQL Server, Oracle, etc.
What is SQL?
The Data Definition Language (DDL) part of SQL permits database tables to be created or deleted. pecify links between tables, and impose constraints between database tables.
The most important DDL statements in SQL are:
•CREATE TABLE - creates a new database table •ALTER TABLE - alters (changes) a database table •DROP TABLE - deletes a database table •CREATE INDEX - creates an index (search key) •DROP INDEX - deletes an index
Data Definition Language (DDL)
The Data Manipulation Language (DML) part of SQL permits database users to insert, delete and update data in a database.
The most important DML statements in SQL are:
•INSERT INTO – insert new records into the database table •UPDATE – changes the content of the database table •DELETE – deletes records from the database table
Data Manipulation Language (DML)
Basic structure of an SQL queryBasic structure of an SQL query
General Structure SELECT, ALL / DISTINCT, *,AS, FROM, WHERE
Comparison IN, BETWEEN, LIKE "% _"Grouping GROUP BY, HAVING,
COUNT( ), SUM( ), AVG( ), MAX( ), MIN( )Logical Operators AND, OR, NOTDisplay Order ORDER BY, ASC / DESC
◦ The query will select rows from the source tablename and output the result in table form.
– expr1, expr2,… are Expressions– col1, col2 are their corresponding column names in the
output table.– DISTINCT will eliminate duplication in the output
while ALL will keep all duplicated rows.– condition can be :
• an inequality, or• a string comparison• using logical operators AND, OR, NOT.
SELECTSELECT [[ALL / DISTINCTALL / DISTINCT] ] expr1expr1 [ [ASAS col1col1], ], expr2expr2 [ [ASAS col2col2] ] ;;
FROMFROM tablenametablename WHEREWHERE conditioncondition
fieldfield typetype widthwidth contentscontentsid numeric 4 student id numbername character 25 namedob date 8 date of birthsex character 1 sex: M / Fclass character 2 classemark numeric 2 entrance Mark city character 20 city
Student Table
eg. 1 List all the student records.SELECT * FROM student
id name dob sex Dept emark City9801 Anitha 06/04/90 F Mechanical 70 Chennai9802 Bindhu 01/10/90 F Electrical 92 Bangalore9803 Vimal 03/16/90 M Mechanical 91 Chennai9804 Kannan 07/09/90 F Mechanical 84 Mumbai9805 Ram 10/20/90 M Electrical 88 Chennai
: : : : : :
Result
eg. 2 List the names,dept and emark of Electrical students:
SELECT name,dept, emark, dept FROM student ;
WHERE dept=“Electrical"
name dept emarkBindhu Electrical 92Ram Electrical 88
Result
eg. 3 List the names and ages of Electrical girls.
Electrical Girls ?Electrical Girls ?
Condition for “Electrical Girls":Condition for “Electrical Girls":
1)1) class = class = " Electrical "" Electrical "
2)2) sex = sex = "F""F"
3)3) Both ( AND operator)Both ( AND operator)
eg. 3 List the names and ages of Electrical girls.
What is "age“?What is "age“?
Functions:Functions:
# days :# days : DATE( ) – dobDATE( ) – dob
# years :# years : (DATE( ) – dob) / 365(DATE( ) – dob) / 365
1 d.p.1 d.p. :: ROUND(__ , 0)ROUND(__ , 0)
eg. 3 List the names and ages of Electrical girls.
SELECT name, ROUND((DATE( )-dob)/365,0) AS age FROM student WHERE class=“Electrical" AND sex="F"
name ageBindhu 21.0
: :
Result
Comparison
expr IN ( value1, value2, value3)expr BETWEEN value1 AND value2expr LIKE "%_"
eg. 4 List the students who were born on Wednesday or Saturdays.
SELECT name, dept, CDOW(dob) AS bdate FROM student WHERE DOW(dob) IN (4,7)
name dept bdateBindhu Electrical WednesdayRam Electrical Saturday
Result
eg. 5 List the students whose names start with “R".SELECT name, dept FROM student ;
WHERE name LIKE “R%"
name deptRam Electrical
: :Result
GroupingSELECT ...... FROM ...... WHERE condition ;GROUP BY groupexpr [HAVING requirement]
Group functions: COUNT( ), SUM( ), AVG( ), MAX( ), MIN( )
• groupexpr specifies the related rows to be grouped as one entry. Usually it is a column.
• WHERE condition specifies the condition of individual rows before the rows are group. HAVING requirement specifies the condition involving the whole group.
dept cntMechanical 3Electrical 2
eg. 6 List the number of students of each dept.SELECT dept, COUNT(*) FROM student
GROUP BY dept
Result
Group By deptGroup By dept
AVG( )AVG( )
AVG( )AVG( )
ElectricalElectrical
MechanicalMechanical
MechanicalMechanical
MechanicalMechanical
MechanicalMechanical
MechanicalMechanical
MechanicalMechanical
StudentStudent
dept
ElectricalElectrical
ElectricalElectrical
ElectricalElectrical
eg. 7 List the average entrance test score of each dept.
SELECT dept, AVG(etest) FROM student GROUP BY dept
dept avg_etestElectrical 90.00
Mechanical 81.66Result
Grouping - Having
eg. 8 List the average Entrance test score of the boys in each class. The list should not contain dept with less than 3 boys.
SELECT AVG(mtest), class FROM student ;WHERE sex="M" GROUP BY dept ;HAVING COUNT(*) >= 3
Display Order
SELECT ...... FROM ...... WHERE ...... SELECT ...... FROM ...... WHERE ...... GROUP BY ..... ;GROUP BY ..... ;ORDER BY ORDER BY colnamecolname ASC / DESC ASC / DESC
Natural Join
A Natural Join is a join operation that joins two tables by their common column. This operation is similar to the setting relation of two tables.
SELECT a.comcol, a.col1, b.col2, expr1, expr2 ;FROM table1 a, table2 b ;WHERE a.comcol = b.comcol
MusicMusic
idid
98019801
typetype
StudentStudent
98019801
idid namename DEPTDEPT
98019801
ProductProduct
idid namename DEPTDEPT typetype
Same idSame id
JoinJoin
Natural Join
Thank YouThank You