Download presentation
Presentation is loading. Please wait.
Published byMildred Spencer Modified over 6 years ago
1
Working with Tables: Join, Functions and Grouping
Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008
2
RETRIEVING DATA FROM A TABLE
The main purpose of the SQL language is for querying the database The most important statement or query is the SELECT query The general syntax is SELECT columnlist FROM tablename;
3
RETRIEVING DATA FROM A TABLE
Example: SELECT Last, First FROM student;
4
RETRIEVING DATA FROM A TABLE
SELECT (*) If you want to see all columns in a table, you do not have to list them all. You can use character asterisk (*) in place of the column list, and all columns will be displayed in the same order as the underlying table structure.
5
RETRIEVING DATA FROM A TABLE
RESTRICTING DATA WITH A WHERE CLAUSE A WHERE clause is used with the SELECT query to restrict rows picked The general syntax of the WHERE clause is SELECT columnlist FROM tablename [WHERE condition(s)];
6
RETRIEVING DATA FROM A TABLE
SELECT * FROM dept WHERE Location = ‘Monroe’; SELECT Lname, Fname, Salary, Deptid FROM employee WHERE Salary >= 50000;
7
RETRIEVING DATA FROM A TABLE
SELECT Lname, Fname, Salary, Deptid FROM employee WHERE Salary >= AND Salary <=25000; WHERE Salary BETWEEN AND 25000;
8
RETRIEVING DATA FROM A TABLE
SORTING The order of rows in a table is arbitrary. You may want to see rows in a specific order based on a column or columns For example, you may want to see employees in alphabetical order by their name
9
RETRIEVING DATA FROM A TABLE
The ORDER BY clause is used with the SELECT query to sort rows in a table. The general syntax is SELECT columnlist FROM tablename [WHERE condition(s)] [ORDER BY column|expression [ASC|DESC]];
10
RETRIEVING DATA FROM A TABLE
SELECT Last, First FROM student ORDER BY Last; ORDER BY Last DESC;
11
Group Functions The group functions perform an operation on a group of rows and return one result. Sum () Finds sum of all values in a column, ignores null values. Avg () Finds average of all values in a column, ignores null values. Max () Finds maximum value and ignores null values. Min () Finds minimum value and ignores null values. Count(), Count(*) Counts number of rows including nulls for *. Counts non-null values if column or expression is used as argument.
12
Group Functions SELECT SUM(Salary), AVG(Salary),
MAX(Salary), MIN(Salary) FROM EMPLOYEE;
13
Grouping Data The rows in a table can be divided into different groups to treat each group separately.
14
Grouping Data The GROUP BY clause is used for grouping data. The general syntax is SELECT column, groupfunction (column) FROM tablename [WHERE condition(s)] [GROUP BY column|expression] [ORDER BY column|expression [ASC|DESC]];
15
Grouping Data SELECT DeptID, COUNT(*) FROM employee GROUP BY DeptID
16
Joins When the required data is in more than one table, related tables are joined using a join condition. In most cases, the common columns are the primary key in one table and a foreign key in another
17
Cartesian Product SELECT Last, First, Name FROM student, faculty;
18
Equijoin The equijoin is a join with a join condition involving common columns from two tables. Join Syntax: SELECT columnnames FROM tablenames WHERE join condition(s);
19
Equijoin SELECT student.Last STUDENT, faculty.Name Faculty
FROM student, faculty WHERE student.FacultyID = faculty.FacultyID
20
Table Aliases SELECT s.Last STUDENT, f.Name Faculty
FROM student s, faculty f WHERE student.FacultyID = faculty.FacultyID
21
Multiple Joins SELECT e.Lname EMPLOYEE, d.DeptName Department,
q.QualDesc QUALIFICATION FROM EMPLOYEE e, Department d, QUALIFICATION q WHERE e.DeptID = d.DeptID AND e.QualID = q.QualID
22
Creating a table using a subquery
You can create a table by using a nested SELECT query. The Query will create a new table and populated it with the rows selected from the other table. CREATE TABLE tablename AS SELECT query
23
Creating a table using a subquery
CREATE TABLE temp AS SELECT Employee ID, Lname, Fname, Salary FROM employee WHERE DeptID=20; DESCRIBE TABLE SELECT * FROM temp;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.