Presentation is loading. Please wait.

Presentation is loading. Please wait.

An Introduction To SQL Part 2 (Special thanks to Geoff Leese)

Similar presentations


Presentation on theme: "An Introduction To SQL Part 2 (Special thanks to Geoff Leese)"— Presentation transcript:

1 An Introduction To SQL Part 2 (Special thanks to Geoff Leese)

2  Recap:  The SQL Data Manipulation Language (DML)has four commands: ◦ SELECT: retrieving data ◦ INSERT: creating data ◦ UPDATE: altering data ◦ DELETE: removing data

3  You can display data in a different format: ◦ Show all the department names found in the DEPT table but rename the column with the heading DIFF_DEPT: SELECT DNAME AS DIFF_DEPT FROM DEPT; DIFF_DEPT --------------------- ACCOUNTING RESEARCH SALES OPERATIONS

4  Oracle will display your rows of data in an unordered fashion.  We use the ORDER BY clause to order the rows that are retrieved.  The ORDER BY clause should always be placed last in the query.  The default ordering is ascending ◦ Numeric ascending by order value ◦ Dates chronological order ◦ Charalphabetically ◦ DESC is used to reverse the order

5  List all departments in order of their location: SELECT * FROM DEPT ORDER BY LOC; DEPTNO DNAMELOC --------- ------------- ------------- 40 OPERATIONS BOSTON 30 SALES CHICAGO 20 RESEARCH DALLAS 10 ACCOUNTINGNEW YORK

6  Show details of employees in department 10 with the earliest hire dates first: SELECT EMPNO, ENAME, HIREDATE FROM EMP WHERE DEPTNO = 10 ORDER BY HIREDATE; EMPNO ENAMEHIREDATE -------- ------- --------- 7782 CLARK 1981-06-09 7839 KING 1981-11-17 7934 MILLER 1982-01-23

7  Show all employees in job order but within each job place in descending order of salary: SELECT ENAME, JOB, SAL, DEPTNO FROM EMP ORDER BY JOB, SAL DESC; ◦ 14 rows will be selected.

8 SELECT ename, job, sal, comm, comm/sal as ratio,deptno From EMP Where deptno = 30 Order by ratio; ENAME JOB SAL COMM RATIO DEPTNO ---------- --------- --------- --------- --------- --------- TURNER SALESMAN 1500 0 0 30 ALLEN SALESMAN 1600 300.1875 30 WARD SALESMAN 1250 500.4 30 MARTIN SALESMAN 1250 1400 1.12 30 BLAKE MANAGER 2850 30 JAMES CLERK 950 30

9  Results can be grouped according to a common attribute value. We can find the sum of the salaries by department. SELECT deptno, sum(sal) FROM EMP GROUP BY deptno; DEPTNO SUM(SAL) ------- --------- 10 8750 20 10875 30 9400

10  Some simple functions include: ◦ Count: returns a count of rows ◦ Min:returns the lowest value for an attribute ◦ Max:returns the highest value for an attribute ◦ Sum:returns the sum of values ◦ Avg:returns the average value SELECT COUNT(*) FROM EMP; COUNT(*) -------- 14 ◦ RETURNS THE NUMBER OF ROWS IN THE EMP TABLE.

11  You can find the highest salary: Select max(sal) from EMP; MAX(SAL) -------- 5000 Select min(sal) as min_sal from EMP; MIN_SAL ------- 800

12  Sum is used on numeric functions to return the sum of all the values: ◦ SELECT sum(sal), sum(comm) from EMP; SUM(SAL) SUM(COMM) -------- 29025 2200 ◦ SELECT avg(SAL) from EMP; AVG(SAL) --------- 2073.2143

13  We can constrain the subgroups that appear by using a HAVING CLAUSE.  We can find the sum of the salaries of each department, but only request those departments with more than 5 employees. SELECT deptno, sum(sal) FROM EMP GROUP BY deptno HAVING count(*)>5; DEPTNO SUM(SAL) ----- --------- 30 9400

14  Useful to put more than one aggregate function on the same row of output.  E.g. “Count the number of people in dept 10 who receive salary, and the number in dept 10 who receive commission” SELECT COUNT(A.SAL) AS COUNTSAL, COUNT(B.COMM) AS COUNTCOMM FROM EMP A, EMP B WHERE A.EMPNO=B.EMPNO AND A.DEPTNO=10;

15  Create Table  CREATE TABLE customer as follows: CREATE TABLE customer (customer_id NUMBER(5) NOT NULL, name VARCHAR2(10), house_number NUMBER(2), street VARCHAR2(15), town VARCHAR2(15));  Each column has a name, data type and column width.

16  Here’s an example. What’s it doing? CREATE TABLE account (account_numnumber(6), account_namevarchar2(20), branch_namevarchar2(20), date_openeddate, balancenumber(10,2), constraint pk_account_num primary key (account_num)); The Balance column is a decimal number with a maximum Column width of ten digits and a precision of two digits i.e. 10.251 is stored as 10.25.

17  A correct definition of the table ‘customer’ might be: CREATE TABLE customer (customer_id NUMBER(5) NOT NULL, name VARCHAR2(10), house_number NUMBER(2), street VARCHAR2(15), town VARCHAR2(15), constraint pk_customer_id primary key (customer_id));

18  The PRIMARY KEY indicates that all values for these columns must be unique.  NOT NULL is used to designate that the column must have a value for all its rows.

19  Customer and account has a many to many relationship, we can resolve this with a table constraint by creating a composite primary key. CREATE table customer_account (customer_id number(5) NOT NULL, account_num number(6) NOT NULL, constraint pk_cust_account PRIMARY KEY (customer_id, account_num));

20  In the CUSTOMER_ACCOUNT table that we have just created we have two foreign keys CUSTOMER_ID and ACCOUNT_NUM. We can specify these after creating the table: ALTER table customer_account ADD (constraint fk_customer_id FOREIGN KEY (customer_id) REFERENCES customer (customer_id)); ALTER table customer_account ADD (constraint fk_account_num FOREIGN KEY(account_num) REFERENCES account(account_num));

21  DROP <object name  DROP TABLE EMP; (NOT undoable!)  ALTER object name> ADD /DROP COLUMN  ALTER TABLE EMP  ADD nickname VARCHAR(30);

22  We use the SQL command INSERT to create rows. INSERT INTO customer VALUES(12345, 'DAWES', 21, 'BACK LANE', 'STAFFORD'); ◦ Remember non-numerics require quotation marks around their values. ◦ These values have been entered into the Customer table in the order in which the columns appeared in the original create table statement.

23  Use the DELETE command to remove data from a relation (i.e. table). DELETE FROM customer WHERE customer_id = 12345;  Use the UPDATE command to alter the data held in the rows. i.e. Change the name in the account table from Dawes to Dalby. UPDATE customer SET name = 'DALBY' WHERE customer_id = 12345;

24  You have worked through some simple SQL expressions and have learnt how to : ◦ Use the SELECT command for retrieving data. ◦ Use INSERT, UPDATE, AND DELETE commands to insert, amend and delete data. ◦ Use aggregate functions to assist with processing data, i.e. COUNT, MIN, MAX, SUM, AVG. ◦ Use DDL to create, alter and drop tables. ◦ Create some simple constraints using the WHERE clause. ◦ Use the GROUP BY, HAVING and ORDER BY functions to assist with selecting and displaying data. ◦ Create simple primary and foreign key constraints.

25  Rolland chapter 5  Patrick chapters 1-3, 9-11, 13  SQL at w3schools - click to follow the link SQL at w3schools - click to follow the link


Download ppt "An Introduction To SQL Part 2 (Special thanks to Geoff Leese)"

Similar presentations


Ads by Google