0% found this document useful (0 votes)
127 views

Database SQL

SQL is a query language that allows users to specify conditions to retrieve data from database programs like MS Access, DB2, MS SQL Server, Oracle, etc. It has three main components: Data Definition Language (DDL) to create/delete tables and constraints; Data Manipulation Language (DML) to insert/update/delete data; and queries to select data using conditions, comparisons, grouping, and ordering. Basic queries use SELECT, FROM, WHERE and can include functions, comparisons, grouping, and ordering to retrieve the desired data.

Uploaded by

athangaraj87
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
127 views

Database SQL

SQL is a query language that allows users to specify conditions to retrieve data from database programs like MS Access, DB2, MS SQL Server, Oracle, etc. It has three main components: Data Definition Language (DDL) to create/delete tables and constraints; Data Manipulation Language (DML) to insert/update/delete data; and queries to select data using conditions, comparisons, grouping, and ordering. Basic queries use SELECT, FROM, WHERE and can include functions, comparisons, grouping, and ordering to retrieve the desired data.

Uploaded by

athangaraj87
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 24

Structured Query Language

What is SQL?

– 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.
Data Definition Language (DDL)

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 Manipulation Language (DML)

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
Basic 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, NOT
Display Order ORDER BY, ASC / DESC
SELECT [ALL / DISTINCT] expr1 [AS col1], expr2 [AS col2] ;
FROM tablename WHERE condition
◦ 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.
Student Table

field type width contents


id numeric 4 student id number
name character 25 name
dob date 8 date of birth
sex character 1 sex: M / F
class character 2 class
emark numeric 2 entrance Mark
city character 20 city
eg. 1 List all the student records.
SELECT * FROM student

id name dob sex Dept emark City


9801 Anitha 06/04/90 F Mechanical 70 Chennai
9802 Bindhu 01/10/90 F Electrical 92 Bangalore
Result 9803 Vimal 03/16/90 M Mechanical 91 Chennai
9804 Kannan 07/09/90 F Mechanical 84 Mumbai
9805 Ram 10/20/90 M Electrical 88 Chennai
: : : : : :
eg. 2 List the names,dept and emark of Electrical
students:

SELECT name,dept, emark, dept FROM student ;


WHERE dept=“Electrical"

name dept emark


Result Bindhu Electrical 92
Ram Electrical 88
eg. 3 List the names and ages of Electrical girls.

Electrical Girls ?

Condition for “Electrical Girls":

1) class = " Electrical "

2) sex = "F"

3) Both ( AND operator)


eg. 3 List the names and ages of Electrical girls.

What is "age“?

Functions:

# days : DATE( ) – dob

# years : (DATE( ) – dob) / 365

1 d.p. : 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 age
Result Bindhu 21.0
: :
Comparison
expr IN ( value1, value2, value3)
expr BETWEEN value1 AND value2
expr 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 bdate


Result Bindhu Electrical Wednesday
Ram Electrical Saturday
eg. 5 List the students whose names start with “R".
SELECT name, dept FROM student ;
WHERE name LIKE “R%"

name dept
Result Ram Electrical
: :
Grouping
SELECT ...... 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.
eg. 6 List the number of students of each dept.
SELECT dept, COUNT(*) FROM student
GROUP BY dept

dept cnt
Result Mechanical 3
Electrical 2
Group By dept
dept
Electrical

Electrical Electrical AVG( )


Electrical

Mechanical

Mechanical

Mechanical Mechanical

Mechanical
AVG( )
Mechanical

Student
eg. 7 List the average entrance test score of each dept.

SELECT dept, AVG(etest) FROM student


GROUP BY dept

dept avg_etest
Result Electrical 90.00
Mechanical 81.66
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 ......
GROUP BY ..... ;
ORDER BY colname 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
Natural Join

id name DEPT id type

Same id 9801
9801

Join
Student Music

id name DEPT type

9801
Product
Thank You

You might also like