Download presentation
Presentation is loading. Please wait.
Published byNathaniel Smith Modified over 9 years ago
1
MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 4 © Akhilesh Bajaj, 2000, 2002, 2004, 2008. All Rights Reserved.
2
Goals for Today Learn the basics of the query component of SQL - the SELECT clause - the WHERE clause - the FROM clause - the AS clause - the LIKE operator - the ORDER BY clause - the UNION operation - the INTERCEPT operation - the MINUS operation
3
Structured Query Language (SQL) The standard relational database language Used to query the database (hardest to learn!), to define the relational schema, and to insert, delete and update data in the database. Two standards exist: SQL-89 and SQL-92. Most systems support at least SQL-89. SQL is the standard language with which you interact with the relational database system. SQL was invented in the early 1970-s. It has been around for around 25 years, and likely to be around for decades ahead.
4
Structured Query Language (SQL): Query Part To use SQL for querying, we need a defined relational schema to already exist. We also assume there is some data in the tables. SQL allows the use of null values. The basic query clause in SQL is the select from where Condition ; clause. Note that the select here is similar to the project in relational algebra. The where here is similar to the select in relational algebra.
5
Relational Schema For Example SQL Queries Professors (f_id, f_name, f_address, f_specialty, highest_degree) Courses (c_id, c_name, num_credits) Sections (c_id, s_id, time_held, week_days_held, date_began, date_ended, cl_id) Students (st_id, st_name, gender, st_gpa) Grades (st_id, c_id, s_id, grade) Classrooms (cl_id, capacity, location) Teach (f_id, c_id, s_id)
6
Structured Query Language (SQL): Query Part Example: Information on all Professors who have a Ph.D. degree and specialize in “Databases”.
7
Structured Query Language (SQL): Query Part The SELECT clause: The result of an SQL query is a relation (or table). This result table is NOT stored as part of the schema; instead it is either printed on the screen or stored as a file. Note that in SQL the result table permits duplicates by default. This is unlike relational algebra. The SELECT DISTINCT clause forces elimination of duplication. The SELECT ALL clause forces duplication. The * symbol after SELECT means all attributes. Note: * is also used as the “multiply” operator The SELECT clause can also contain arithmetic expressions E.g., SELECT st_gpa*10 FROM students; /*This query gives the GPAs multiplied by 10*/
8
Structured Query Language (SQL): Query Part The WHERE clause: The WHERE clause specifies the conditions that will select all the rows we want. Conditions are formed using >, =,<>, AND, OR, NOT, BETWEEN AND and NOT BETWEEN AND. E.g., SELECT st_name FROM students WHERE st_gpa BETWEEN 2.0 AND 3.0;
9
Structured Query Language (SQL): Query Part The FROM clause: The FROM clause defines a cartesian product of all the tables in the clause. This means that the “garbage” rows have to be explicitly removed using the WHERE clause. This is VERY IMPORTANT. E.g., SELECT f_name FROM professors, teach WHERE professors.f_id = teach.f_id AND teach.c_id = ‘MIS3053’;
10
Structured Query Language (SQL): Query Part The AS clause: The AS clause is used to rename both tables and attributes. Tables are renamed in the FROM clause, attributes in the SELECT clause. E.g., SELECT st_name AS names FROM students WHERE st_gpa > 3.5; SELECT S.st_name AS names FROM students AS T, students AS S WHERE T.st_name = ‘Jill Jones’ AND S.st_gpa > T.st_gpa; Result: names ______ Bob Smith Al Stewart NB: In Oracle, the AS clause for tables is a blank.
11
Structured Query Language (SQL): Query Part The LIKE operator: The LIKE operator is used for string matching. The % character matches any substring. In MS Jet SQL/ Access it is *. The _ (underscore) character matches any single character. E.g., SELECT f_name AS Prof_names FROM professors WHERE f_name LIKE ‘%aj%’;
12
Structured Query Language (SQL): Query Part The ORDER BY clause: The ORDER BY clause orders the rows returned as the result. E.g., SELECT st_name FROM students WHERE st_gpa > 3.0 ORDER BY st_gpa ASC; SELECT st_name FROM students WHERE st_gpa > 3.0 ORDER BY st_gpa ASC, st_id ASC; ORDER BY causes the query to run slowly, since sorting takes up time.
13
Structured Query Language (SQL): Query Part The UNION operation: The UNION operation works identically to that in relational algebra. E.g., (SELECT st_name as all_names FROM students) UNION (SELECT f_name AS all_names FROM professors); The UNION operator automatically eliminates duplicates (unlike the SELECT clause). In other words, duplicates are not the default for union. To enforce duplication, use the UNION ALL operator.
14
Structured Query Language (SQL): Query Part The INTERSECT operation: The INTERSECT operation works identically to that in relational algebra. E.g., (SELECT st_name AS all_names FROM students) INTERSECT (SELECT f_name AS all_names FROM professors);
15
Structured Query Language (SQL): Query Part The MINUS operation: The MINUS operation works identically to that in relational algebra. E.g., (SELECT st_name FROM students) MINUS (SELECT st_name FROM students AS S, grades AS T WHERE S.st_id = T.st_id AND T.c_id = ‘MIS3053’ AND T.s_id = ‘ASpr2006’); NB: MINUS is not supported on all systems.
16
Structured Query Language (SQL): Query Part Some Practice Examples 1. The names an student ids of all students who have an ‘A’ in MIS3053AFall2010. 2. The names of all Professors who have never given a ‘C’. 3. The names of all Professors who teach ‘Johnny Jones’.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.