An overview of structured query language (sql), focusing on selections, joins, insertions, updates, deletes, aggregate functions, and grouping. It covers sql-1 and sql-2 standardization, selection criteria, various join types, table aliasing, insertions, updates and deletes, and aggregate functions. It also mentions schema creation, deletion, and common errors.
Download SQL Document: Selections, Joins, Inserts, Updates, Deletes, Aggregates, Grouping and more Slides Database Management Systems (DBMS) in PDF only on Docsity!
Structured Query Language
Overview
Language
Selections
Joins
Insertions
Updates and Deletes
Aggregate Functions
Grouping
SQL Training Resources
Selections
Selects
select < element > from < table > where < criteria >
select is Relational algebra’s projection operation.
where is relational algebra’s selection operation.
Select does not maintain relational algebra’s uniqueness property. Hence the ‘distinct’ keyword.
Selection Criteria
Numbers/Identities
Can be indicated with an equal sign
Strings
Require single quotes
Can be wildcarded using
% (multichar)
_ (singlechar)
Depending on database implementation, may or may not have to worry about case. - Oracle is case sensitive, SQL Server, Postgres, etc. are not
Joins (Continued)
The previous joins follow the format:
Select < elems > from tableA < joinType > tableB on < criteria > ie: select * from employee left join department on (employee.dept = dept.id);
Also, manual joins can be used
Select < elems > from tableA, tableB where < criteria > ie: select * from employee, department where employee.dept = dept.id; // note, not necessarily same result as above
Table Aliasing
Used in manual joins.
Useful for joining multiple tables, saving
typing in where criteria. Follows format:
select < tableAlias> .property from < tableName> < tableAlias> where < tableAlias >.property = foo;
select p.name, r.balanceOwed from person p, record r where r.patientId = p.id;
Updates and Deletes
Updates
Update < tableName > set < attrib > = < value > where < criteria >
Deletes
Delete from where Leaving out where criteria will delete/Update everything in the table
Aggregate Functions
Count – returns count of matching tuples
Average – returns average of specified attribue
Max – returns max of specified attribute
Min – returns min of specified attribute
Sum – returns sum of all rows in table attribute
Follow format:
select fn (< elem >) from < table > where < criteria >