Lecture04DB.ppt - Introduction to SQL Stands for Structured...

Info icon This preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to SQL Stands for Structured Query Language Standard interface to relational databases SQL is used to create database structure, or to fill it with data, or to remove data or alter data User Friendly Non Procedural Very High Level Query Language SQL SQL Statements Statements SELECT Data retrieval INSERT UPDATE DELETE Data manipulation language (DML) CREATE ALTER Data definition language (DDL) DROP RENAME TRUNCATE COMMIT ROLLBACK SAVEPOINT GRANT REVOKE Transaction control Data control language (DCL) Capabilities Capabilities of of SQL SQL SELECT SELECT Statements Statements Selection Projection Table 1 Table 1 Table 1 Join Table 2 Capabilities Capabilities of of SQL SQL SELECT SELECT Statements Statements A SELECT statement retrieves information from the database. Using a SELECT statement, you can do the following: • Selection • Projection • Join Writing Writing SQL SQL Statements Statements SQL SQL statements statements are are not not case case sensitive. sensitive. SQL statements can be on one or SQL statements can be on one or more more lines. lines. Keywords cannot be abbreviated or split Keywords cannot be abbreviated or split across across lines. lines. Tabs Tabs and and indents indents are are used used to to enhance enhance readability. readability. Guidelines are from Oracle Corporation for Oracle DBMS but also valid for other databases (if not all then also most of them are true) Tables Used in the Lecture Tables Used in the Lecture EMP EMPNO JOB MGR EMPNO ENAME ENAME JOB MGR ----------------- ------------------- ----------------- ----------------7839 KING PRESIDENT 7839 KING PRESIDENT 7698 MANAGER 7839 7698 BLAKE BLAKE MANAGER 7839 7782 MANAGER 7839 7782 CLARK CLARK MANAGER 7839 7566 MANAGER 7839 7566 JONES JONES MANAGER 7839 7654 SALESMAN 7698 7654 MARTIN MARTIN SALESMAN 7698 7499 SALESMAN 7698 7499 ALLEN ALLEN SALESMAN 7698 7844 SALESMAN 7698 7844 TURNER TURNER SALESMAN 7698 7900 CLERK 7698 DEPT 7900 JAMES JAMES CLERK 7698 7521 WARD SALESMAN 7698 7521 SALESMAN 7698 DEPTNO LOC DEPTNO DNAME DNAMEWARD LOC 7902 FORD ANALYST 7566 7902 FORD ANALYST 7566 -------------------------- -----------------------------------7369 SMITH CLERK 7902 7369 10 SMITH CLERK NEW 7902 ACCOUNTING 10 ACCOUNTING NEW 7788 ANALYST 7566 7788 SCOTT SCOTT ANALYST 7566 YORK YORK 7876 ADAMS CLERK 7788 7876 ADAMS CLERK 7788 20 DALLAS 20 RESEARCH RESEARCH DALLAS 7934 MILLER CLERK 7782 7934 CLERK 7782 30 CHICAGO 30 SALES SALESMILLER CHICAGO 40 BOSTON 40 OPERATIONS OPERATIONS BOSTON HIREDATE SAL COMM DEPTNO HIREDATE SAL COMM DEPTNO ----------------- ----------------- ----------------- ----------------17-NOV-81 5000 10 17-NOV-81 5000 10 01-MAY-81 2850 30 01-MAY-81 2850 30 09-JUN-81 2450 10 09-JUN-81 2450 10 02-APR-81 2975 20 02-APR-81 2975 20 28-SEP-81 1250 1400 30 28-SEP-81 1250 1400 30 20-FEB-81 1600 300 30 20-FEB-81 1600 300 30 08-SEP-81 1500 00 30 08-SEP-81 1500 30 03-DEC-81 950 30 03-DEC-81 950 30 22-FEB-81 1250 500 30 22-FEB-81 1250 500 30 03-DEC-81 3000 20 03-DEC-81 SALGRADE 3000 20 17-DEC-80 800 20 17-DEC-80 800 20 GRADE LOSAL HISAL GRADE LOSAL HISAL 09-DEC-82 3000 20 09-DEC-82 --------3000 --------20 ----------------- ----------------12-JAN-83 1100 20 12-JAN-83 1100 11 20 700 1200 700 1200 23-JAN-82 1300 10 23-JAN-82 1300 22 10 1201 1400 1201 1400 33 1401 2000 1401 2000 44 2001 3000 2001 3000 55 3001 9999 3001 9999 Basic Basic SELECT SELECT Statement Statement SELECT <column SELECT <column list> list> FROM FROM table; table; SELECT SELECT identifies identifies what what columns(similar columns(similar to to projection projection operation). operation). FROM identifies which table. FROM identifies which table. Selecting Selecting All All Columns Columns SQL> SELECT * 2 FROM dept; DEPTNO --------10 20 30 40 DNAME -------------ACCOUNTING RESEARCH SALES OPERATIONS LOC ------------NEW YORK DALLAS CHICAGO BOSTON Selecting Selecting Specific Specific Columns Columns SQL> SELECT deptno, loc 2 FROM dept; DEPTNO --------10 20 30 40 LOC ------------NEW YORK DALLAS CHICAGO BOSTON Projection Eliminating Eliminating Duplicate Duplicate Rows Rows Eliminate duplicate rows by using the DISTINCT keyword in the SELECT clause. SQL> SELECT DISTINCT deptno 2 FROM emp; DEPTNO --------10 20 30 Limiting Limiting Rows Rows Selected Selected Restrict Restrict the the rows rows returned returned by by using using the the WHERE WHERE clause. clause. SELECT FROM [WHERE column1,column2,…. table condition(s)]; The The WHERE WHERE clause clause follows follows the the FROM FROM clause. clause. Using Using the the WHERE WHERE Clause Clause SQL> SELECT ename, job, deptno 2 FROM emp 3 WHERE job='CLERK'; ENAME ---------JAMES SMITH ADAMS MILLER JOB DEPTNO --------- --------CLERK 30 CLERK 20 CLERK 20 CLERK 10 Character Character Strings Strings and and Dates Dates Character Character strings strings and and date date values values are are enclosed enclosed in in single single quotation quotation marks. marks. Character Character values values are are case case sensitive sensitive and and date date values values are are format format sensitive. sensitive. SQL> SQL> 22 33 SELECT SELECT FROM FROM WHERE WHERE ename, ename, job, job, deptno deptno emp emp ename ename == 'JAMES'; 'JAMES'; Comparison Comparison Operators Operators Operator Meaning = Equal to > Greater than >= Greater than or equal to < Less than <= Less than or equal to <> Not equal to Using Using the the Comparison Comparison Operators Operators SQL> SELECT ename, sal, comm 2 FROM emp 3 WHERE sal<=comm; ENAME SAL COMM ---------- --------- --------MARTIN 1250 1400 Other Other Comparison Comparison Operators Operators Operator Meaning BETWEEN ...AND... Between two values (inclusive) IN(list) Match any of a list of values LIKE Match a character pattern IS NULL Is a null value Using Using the the BETWEEN BETWEEN Operator Operator Use Use the the BETWEEN BETWEEN operator operator to to display display rows rows based based on on aa range range of of values. values. SQL> SELECT 2 FROM 3 WHERE ename, sal emp sal BETWEEN 1000 AND 1500; ENAME SAL ---------- --------MARTIN 1250 TURNER 1500 WARD 1250 ADAMS 1100 MILLER 1300 Lower limit Higher limit Using Using the the IN IN Operator Operator Use Use the the IN IN operator operator to to test test for for values values in in aa list. list. SQL> SELECT 2 FROM 3 WHERE EMPNO --------7902 7369 7788 7876 empno, ename, sal, mgr emp mgr IN (7902, 7566, 7788); ENAME SAL MGR ---------- --------- --------FORD 3000 7566 SMITH 800 7902 SCOTT 3000 7566 ADAMS 1100 7788 Defining Defining aa Column Column Alias Alias Renames Renames aa column column heading heading Is useful with calculations Is useful with calculations Immediately follows column name; Immediately follows column name; AS AS keyword keyword between between column column name name and and alias alias Using Using Column Column Aliases Aliases SQL> SELECT ename AS name, sal AS salary FROM emp; NAME SALARY ------------- --------... SQL> SELECT ename Name, sal*12 AS AnnualSalary FROM emp; Name AnnualSalary ------------- ------------... Select-From-Where Statements The principal form of a query is: SELECT desired attributes FROM one or more tables WHERE condition about tuples of the tables ...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern