<ul><li>Introduction to SQL </li></ul><ul><li>SQL Definition </li></ul><ul><li>SQL Manipulation </li></ul><ul><li>SQL Quer...
<ul><li>SQL Order By </li></ul><ul><li>SQL AND & OR </li></ul><ul><li>SQL In </li></ul><ul><li>SQL Between </li></ul><ul><...
Introduction to SQL <ul><li>Stands for  S tructured  Q uery  L anguage </li></ul><ul><li>SQL is a standard computer langua...
<ul><li>Statements for defining database or table </li></ul><ul><ul><li>CREATE </li></ul></ul><ul><ul><li>ALTER </li></ul>...
<ul><li>Statements to add, modify and remove rows  in </li></ul><ul><li>tables. </li></ul><ul><ul><li>INSERT </li></ul></u...
<ul><li>Statements used to retrieve data. </li></ul><ul><ul><li>SELECT </li></ul></ul>
<ul><li>Statements used for database security. </li></ul><ul><ul><li>GRANT </li></ul></ul><ul><ul><li>REVOKE </li></ul></ul>
<ul><li>Example 1: </li></ul><ul><li>To create a database: </li></ul>CREATE DATABASE database_name   <ul><li>Example 2: </...
Some Basic Data Types SQL Create Data Type Description integer (size) int (size) smallint (size) tinyint (size) Hold integ...
Some Basic Data Types (cont.) SQL Create char (size) Holds a fixed length string (can contain letters, numbers, and specia...
<ul><li>Create a database named DREAMHOME </li></ul><ul><li>CREATE DATABASE Dreamhome </li></ul><ul><li>Create these table...
<ul><li>Branch Table </li></ul><ul><li>CREATE TABLE Branch </li></ul><ul><li>(branchNo char(10) NOT NULL, </li></ul><ul><l...
<ul><li>Viewing table </li></ul><ul><li>CREATE TABLE Viewing </li></ul><ul><li>(clientNo char(10) NOT NULL, </li></ul><ul>...
<ul><li>Registration Table </li></ul><ul><li>CREATE TABLE Registration </li></ul><ul><li>(clientNo char(10) NOT NULL, </li...
ALTER Statement
<ul><li>Used to add or delete a column in an existing table </li></ul><ul><li>Adding column </li></ul><ul><li>ALTER TABLE ...
<ul><li>Add a column in Viewing named  NoOfViews </li></ul><ul><li>ALTER TABLE Viewing </li></ul><ul><li>ADD NoOfViews int...
DROP Statement
<ul><li>Used to delete a database or table. </li></ul><ul><li>Deleting a database </li></ul><ul><li>DROP DATABASE <databas...
<ul><li>Delete the tables Viewing and Registration </li></ul><ul><li>DROP TABLE Viewing </li></ul><ul><li>DROP TABLE Regis...
INSERT Statement
<ul><li>Used to insert new rows into a table. The columns to be inserted with data can also be specified. </li></ul><ul><l...
<ul><li>Add a new row in Branch with the following values:  </li></ul><ul><li>branchNo – B001 </li></ul><ul><li>Street – 4...
<ul><li>Insert the following values in Viewing: </li></ul><ul><li>clientNo – CR74 </li></ul><ul><li>propertyNo – PL94 </li...
UPDATE Statement
<ul><li>Used to modify a row in a table </li></ul><ul><li>UPDATE <table_name> </li></ul><ul><li>SET <column_name> = <value...
<ul><li>In the Viewing table, change the comment of all rows with clientNo = CR56 to “Big rooms” </li></ul><ul><li>UPDATE ...
DELETE Statement
<ul><li>Used to delete a row in a table </li></ul><ul><li>DELETE FROM <table_name> </li></ul><ul><li>WHERE <column_name> =...
<ul><li>From Branch table, delete rows where city = London </li></ul><ul><li>DELETE FROM Branch </li></ul><ul><li>WHERE Ci...
SELECT Statement
<ul><li>Used to retrieve one or more columns from a table </li></ul><ul><li>SELECT [DISTINCT] <* | {column_name[,] }> </li...
<ul><li>Display all columns in PrivateOwner table </li></ul><ul><li>SELECT * from PrivateOwner </li></ul><ul><li>Display a...
<ul><li>Display the first name, last name, and position of all Staff  </li></ul><ul><li>SELECT fName, lName, Position FROM...
<ul><li>After completing this lesson, you should be able to do the following: </li></ul><ul><ul><li>List the capabilities ...
Selection Projection Table 1 Table 2 Table 1 Table 1 Join
<ul><ul><li>SELECT identifies  what  columns. </li></ul></ul><ul><ul><li>FROM identifies  which  table. </li></ul></ul>SEL...
<ul><ul><li>SQL statements are not case sensitive.  </li></ul></ul><ul><ul><li>SQL statements can be on one or more lines....
DEPTNO DNAME  LOC --------- -------------- ------------- 10 ACCOUNTING  MALOLOS 20 RESEARCH  HAGONOY 30 SALES  CALUMPIT 40...
DEPTNO LOC --------- ------------- 10 MALOLOS 20 HAGONOY 30 CALUMPIT 40 PULILAN SQL> SELECT deptno, loc 2  FROM  dept;
<ul><li>Create expressions on NUMBER and DATE data by using arithmetic operators. </li></ul>Operator + - * /  Description ...
SQL> SELECT ename, sal, sal+300 2  FROM emp; ENAME  SAL  SAL+300 ---------- --------- --------- ROMEL 20000   20300 JOI 10...
<ul><ul><li>Multiplication and division take priority over addition and subtraction. </li></ul></ul><ul><ul><li>Operators ...
SQL> SELECT ename, sal, 12*sal+100 2  FROM  emp; ENAME  SAL 12*SAL+100 ---------- --------- ---------- ROMEL 20000   24010...
SQL> SELECT ename, sal, 12*(sal+100) 2  FROM  emp; ENAME  SAL 12*(SAL+100) ---------- --------- ----------- ROMEL 20000   ...
<ul><ul><li>A null is a value that is unavailable, unassigned, unknown, or inapplicable. </li></ul></ul><ul><ul><li>A null...
<ul><li>Arithmetic expressions containing a null value evaluate to null. </li></ul>SQL> select ename, 12*sal+comm  2  from...
<ul><ul><li>Renames a column heading </li></ul></ul><ul><ul><li>Is useful with calculations </li></ul></ul><ul><ul><li>Imm...
SQL> SELECT ename AS name, sal salary 2  FROM  emp; NAME  SALARY ------------- --------- ... SQL> SELECT ename  &quot;Name...
<ul><ul><li>Concatenates columns or character strings to other columns  </li></ul></ul><ul><ul><li>Is represented by  Plus...
SQL> SELECT ename + job AS &quot;Employees&quot; 2  FROM  emp; Employees ------------------- ROMELPRESIDENT JOISALES MANAG...
<ul><ul><li>A literal is a character, a number, or a date included in the SELECT list. </li></ul></ul><ul><ul><li>Date and...
Employee Details ------------------------- ... 10 rows selected. SQL> SELECT ename + ' is a ‘ + job  2   AS &quot;Employee...
<ul><li>The default display of queries is all rows, including duplicate rows. </li></ul>SQL> SELECT deptno 2  FROM  emp; D...
Eliminate duplicate rows by using the DISTINCT keyword in the SELECT clause. SQL> SELECT DISTINCT deptno 2  FROM  emp; DEP...
SQL statements <ul><li>SQL  </li></ul><ul><ul><li>A language </li></ul></ul><ul><ul><li>ANSI standard </li></ul></ul><ul><...
&quot;…retrieve all employees in department 30&quot; EMP EMPNO  ENAME  JOB  ...  DEPTNO  1001 ROMEL PRESIDENT   40 1002 JO...
<ul><ul><li>Restrict the rows returned by using the WHERE clause. </li></ul></ul><ul><ul><li>The WHERE clause follows the ...
SQL> SELECT ename, deptno 2  FROM  emp 3  WHERE  deptno=10; ENAME  DEPTNO ---------- --------- --------- RANDY   10 MARK  ...
Operator = > >= < <= <> Meaning Equal to Greater than  Greater than or equal to  Less than  Less than or equal to Not equa...
SQL> SELECT ename,comm 2  FROM  emp 3  WHERE  comm>=700; ENAME  COMM ---------- ---------  MARTIN  1000
Operator BETWEEN ...AND... IN(list) LIKE IS NULL Meaning Between two values (inclusive) Match any of a list of values  Mat...
<ul><li>Use the BETWEEN operator to display rows based on a range of values. </li></ul>ENAME  SAL ---------- --------- JOI...
<ul><li>Use the IN operator to test for values in a list. </li></ul>SQL> SELECT ename, deptno 2  FROM  emp 3  WHERE deptno...
<ul><ul><li>Use the LIKE operator to perform wildcard searches of valid search string values. </li></ul></ul><ul><ul><li>S...
<ul><ul><li>You can combine pattern-matching characters. </li></ul></ul><ul><ul><li>You can use the ESCAPE identifier to s...
<ul><li>Test for null values with the IS NULL operator. </li></ul>SQL> SELECT  ename, mgr 2  FROM  emp 3  WHERE  mgr IS NU...
Operator AND OR NOT Meaning Returns TRUE if  both  component conditions are TRUE Returns TRUE if  either  component condit...
Upcoming SlideShare
Loading in …5
×

Select To Order By

1,321 views

Published on

DBMS2

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,321
On SlideShare
0
From Embeds
0
Number of Embeds
368
Actions
Shares
0
Downloads
105
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Select To Order By

  1. 2. <ul><li>Introduction to SQL </li></ul><ul><li>SQL Definition </li></ul><ul><li>SQL Manipulation </li></ul><ul><li>SQL Query </li></ul><ul><li>SQL Control </li></ul>
  2. 3. <ul><li>SQL Order By </li></ul><ul><li>SQL AND & OR </li></ul><ul><li>SQL In </li></ul><ul><li>SQL Between </li></ul><ul><li>SQL Aliases </li></ul><ul><li>SQL Join </li></ul>SQL ADVANCED <ul><li>SQL Create </li></ul><ul><li>SQL Drop </li></ul><ul><li>SQL Alter </li></ul><ul><li>SQL Group By </li></ul><ul><li>SQL Select Into </li></ul>
  3. 4. Introduction to SQL <ul><li>Stands for S tructured Q uery L anguage </li></ul><ul><li>SQL is a standard computer language for accessing and manipulating databases. </li></ul>What is SQL? <ul><li>Defined by syntax and semantics. </li></ul><ul><li>SQL statements are not case sensitive. </li></ul><ul><li>SQL statements can be one or more lines. </li></ul><ul><li>SQL can retrieve, insert , delete or update records from a database </li></ul><ul><li>SQL is easy to learn </li></ul>
  4. 5. <ul><li>Statements for defining database or table </li></ul><ul><ul><li>CREATE </li></ul></ul><ul><ul><li>ALTER </li></ul></ul><ul><ul><li>DROP </li></ul></ul>
  5. 6. <ul><li>Statements to add, modify and remove rows in </li></ul><ul><li>tables. </li></ul><ul><ul><li>INSERT </li></ul></ul><ul><ul><li>UPDATE </li></ul></ul><ul><ul><li>DELETE </li></ul></ul>
  6. 7. <ul><li>Statements used to retrieve data. </li></ul><ul><ul><li>SELECT </li></ul></ul>
  7. 8. <ul><li>Statements used for database security. </li></ul><ul><ul><li>GRANT </li></ul></ul><ul><ul><li>REVOKE </li></ul></ul>
  8. 9. <ul><li>Example 1: </li></ul><ul><li>To create a database: </li></ul>CREATE DATABASE database_name <ul><li>Example 2: </li></ul><ul><li>To display the companies in reverse alphabetical order: </li></ul>CREATE TABLE table_name ( column_name1 data_type, column_name2 data_type, ....... ) SQL Create The CREATE statement is used to create a new database, table or index.
  9. 10. Some Basic Data Types SQL Create Data Type Description integer (size) int (size) smallint (size) tinyint (size) Hold integers only. The maximum number of digits are specified in parenthesis. Decimal (size, d) numeric (size, d) Hold numbers with fractions. The maximum number of digits are specified in &quot;size&quot;. The maximum number of digits to the right of the decimal is specified in &quot;d&quot;.
  10. 11. Some Basic Data Types (cont.) SQL Create char (size) Holds a fixed length string (can contain letters, numbers, and special characters). The fixed size is specified in parenthesis. varchar (size) Holds a variable length string (can contain letters, numbers, and special characters). The maximum size is specified in parenthesis. date (yyyymmdd) Holds a date
  11. 12. <ul><li>Create a database named DREAMHOME </li></ul><ul><li>CREATE DATABASE Dreamhome </li></ul><ul><li>Create these tables in Dreamhome: </li></ul><ul><ul><li>Branch </li></ul></ul><ul><ul><li>Viewing </li></ul></ul><ul><ul><li>Registration </li></ul></ul>
  12. 13. <ul><li>Branch Table </li></ul><ul><li>CREATE TABLE Branch </li></ul><ul><li>(branchNo char(10) NOT NULL, </li></ul><ul><li> Street char(20), </li></ul><ul><li>City char(10), </li></ul><ul><li>Postcode char(10) </li></ul><ul><li>) </li></ul>
  13. 14. <ul><li>Viewing table </li></ul><ul><li>CREATE TABLE Viewing </li></ul><ul><li>(clientNo char(10) NOT NULL, </li></ul><ul><li> propertyNo char(10), </li></ul><ul><li> viewDate datetime, </li></ul><ul><li> comment char(20) </li></ul><ul><li>) </li></ul>
  14. 15. <ul><li>Registration Table </li></ul><ul><li>CREATE TABLE Registration </li></ul><ul><li>(clientNo char(10) NOT NULL, </li></ul><ul><li> branchNo char(10), </li></ul><ul><li> staffNo char(10), </li></ul><ul><li> dateJoined datetime </li></ul><ul><li>) </li></ul>
  15. 16. ALTER Statement
  16. 17. <ul><li>Used to add or delete a column in an existing table </li></ul><ul><li>Adding column </li></ul><ul><li>ALTER TABLE <table_name> </li></ul><ul><li>ADD <column_name> <data_type> </li></ul><ul><li>Deleting column </li></ul><ul><li>ALTER TABLE <table_name> </li></ul><ul><li>DROP <column_name> <data_type> </li></ul>
  17. 18. <ul><li>Add a column in Viewing named NoOfViews </li></ul><ul><li>ALTER TABLE Viewing </li></ul><ul><li>ADD NoOfViews int </li></ul><ul><li>Delete the column comment in Viewing </li></ul><ul><li>ALTER TABLE Viewing </li></ul><ul><li>DROP comment char </li></ul>
  18. 19. DROP Statement
  19. 20. <ul><li>Used to delete a database or table. </li></ul><ul><li>Deleting a database </li></ul><ul><li>DROP DATABASE <database_name> </li></ul><ul><li>Deleting a table </li></ul><ul><li>DROP TABLE <table_name> </li></ul>
  20. 21. <ul><li>Delete the tables Viewing and Registration </li></ul><ul><li>DROP TABLE Viewing </li></ul><ul><li>DROP TABLE Registration </li></ul><ul><li>Delete the Dreamhome database </li></ul><ul><li>DROP DATABASE Dreamhome </li></ul>
  21. 22. INSERT Statement
  22. 23. <ul><li>Used to insert new rows into a table. The columns to be inserted with data can also be specified. </li></ul><ul><li>Insert new rows </li></ul><ul><li>INSERT INTO <table_name> </li></ul><ul><li>VALUES (<value1,value2,value3,…>) </li></ul><ul><li>Specify columns </li></ul><ul><li>INSERT INTO <table_name> (<column1>,<column2>,…) </li></ul><ul><li>VALUES (<value1>, <value2>,…) </li></ul>
  23. 24. <ul><li>Add a new row in Branch with the following values: </li></ul><ul><li>branchNo – B001 </li></ul><ul><li>Street – 4 New St. </li></ul><ul><li>City – New York </li></ul><ul><li>Postcode – NYC11US </li></ul><ul><li>INSERT INTO Branch </li></ul><ul><li>Values (‘B001’, ’4 New St.’, ’New York’, ‘NYC11US’) </li></ul>
  24. 25. <ul><li>Insert the following values in Viewing: </li></ul><ul><li>clientNo – CR74 </li></ul><ul><li>propertyNo – PL94 </li></ul><ul><li>viewDate – 12-Dec-01 </li></ul><ul><li>INSERT INTO Viewing (clientNo, propertyNo, viewDate) </li></ul><ul><li>VALUES (‘CR74’, ‘PL94’, ‘12-Dec-01’) </li></ul>
  25. 26. UPDATE Statement
  26. 27. <ul><li>Used to modify a row in a table </li></ul><ul><li>UPDATE <table_name> </li></ul><ul><li>SET <column_name> = <value> </li></ul><ul><li>WHERE <column_name> = <some_value> </li></ul>
  27. 28. <ul><li>In the Viewing table, change the comment of all rows with clientNo = CR56 to “Big rooms” </li></ul><ul><li>UPDATE Viewing </li></ul><ul><li>SET comment=‘Big rooms’ </li></ul><ul><li>WHERE clientNo = ‘CR56’ </li></ul>
  28. 29. DELETE Statement
  29. 30. <ul><li>Used to delete a row in a table </li></ul><ul><li>DELETE FROM <table_name> </li></ul><ul><li>WHERE <column_name> = <value> </li></ul><ul><li>Used to delete all rows from a table </li></ul><ul><li>DELETE FROM <table_name> </li></ul>
  30. 31. <ul><li>From Branch table, delete rows where city = London </li></ul><ul><li>DELETE FROM Branch </li></ul><ul><li>WHERE City = ‘London’ </li></ul><ul><li>Delete all rows in Branch table </li></ul><ul><li>DELETE FROM Branch </li></ul>
  31. 32. SELECT Statement
  32. 33. <ul><li>Used to retrieve one or more columns from a table </li></ul><ul><li>SELECT [DISTINCT] <* | {column_name[,] }> </li></ul><ul><li>FROM <table_name> </li></ul><ul><li>Used to retrieve one or more columns from a table based on a certain value </li></ul><ul><li>SELECT [DISTINCT] <* | {column_name[,] }> </li></ul><ul><li>FROM <table_name> </li></ul><ul><li>WHERE <column_name> = <value> </li></ul><ul><li>*DISTINCT – prevents display of duplicate values </li></ul>
  33. 34. <ul><li>Display all columns in PrivateOwner table </li></ul><ul><li>SELECT * from PrivateOwner </li></ul><ul><li>Display all columns in Viewing where clientNo is CR56 </li></ul><ul><li>SELECT * from Viewing </li></ul><ul><li>WHERE clientNo = ‘CR56’ </li></ul>
  34. 35. <ul><li>Display the first name, last name, and position of all Staff </li></ul><ul><li>SELECT fName, lName, Position FROM Staff </li></ul><ul><li>Display the first name, last name, and position of all female Staff </li></ul><ul><li>SELECT fName, lName, Position FROM Staff </li></ul><ul><li>WHERE Sex = ‘F’ </li></ul>
  35. 36. <ul><li>After completing this lesson, you should be able to do the following: </li></ul><ul><ul><li>List the capabilities of SQL SELECT statements </li></ul></ul><ul><ul><li>Execute a basic SELECT statement </li></ul></ul>
  36. 37. Selection Projection Table 1 Table 2 Table 1 Table 1 Join
  37. 38. <ul><ul><li>SELECT identifies what columns. </li></ul></ul><ul><ul><li>FROM identifies which table. </li></ul></ul>SELECT [DISTINCT] {*, column [ alias ],...} FROM table;
  38. 39. <ul><ul><li>SQL statements are not case sensitive. </li></ul></ul><ul><ul><li>SQL statements can be on one or more lines. </li></ul></ul><ul><ul><li>Keywords cannot be abbreviated or split across lines. </li></ul></ul><ul><ul><li>Clauses are usually placed on separate lines. </li></ul></ul><ul><ul><li>Tabs and indents are used to enhance readability. </li></ul></ul>
  39. 40. DEPTNO DNAME LOC --------- -------------- ------------- 10 ACCOUNTING MALOLOS 20 RESEARCH HAGONOY 30 SALES CALUMPIT 40 OPERATIONS PULILAN SQL> SELECT * 2 FROM dept;
  40. 41. DEPTNO LOC --------- ------------- 10 MALOLOS 20 HAGONOY 30 CALUMPIT 40 PULILAN SQL> SELECT deptno, loc 2 FROM dept;
  41. 42. <ul><li>Create expressions on NUMBER and DATE data by using arithmetic operators. </li></ul>Operator + - * / Description Add Subtract Multiply Divide
  42. 43. SQL> SELECT ename, sal, sal+300 2 FROM emp; ENAME SAL SAL+300 ---------- --------- --------- ROMEL 20000 20300 JOI 10000 10300 JANE 5000 5300 ANDY 5875 6175 RANDY 11784 12084 MARK 9775 10075 ... 10 rows selected.
  43. 44. <ul><ul><li>Multiplication and division take priority over addition and subtraction. </li></ul></ul><ul><ul><li>Operators of the same priority are evaluated from left to right. </li></ul></ul><ul><ul><li>Parentheses are used to force prioritized evaluation and to clarify statements. </li></ul></ul>* / + _
  44. 45. SQL> SELECT ename, sal, 12*sal+100 2 FROM emp; ENAME SAL 12*SAL+100 ---------- --------- ---------- ROMEL 20000 240100 JOI 10000 120100 JANE 5000 60100 ANDY 5875 70600 RANDY 11784 141508 ... 10 rows selected.
  45. 46. SQL> SELECT ename, sal, 12*(sal+100) 2 FROM emp; ENAME SAL 12*(SAL+100) ---------- --------- ----------- ROMEL 20000 241200 JOI 10000 121200 JANE 5000 61200 ANDY 5875 71700 RANDY 11784 142608 ... 10 rows selected.
  46. 47. <ul><ul><li>A null is a value that is unavailable, unassigned, unknown, or inapplicable. </li></ul></ul><ul><ul><li>A null is not the same as zero or a blank space. </li></ul></ul>ENAME JOB SAL COMM ---------- --------- --------- --------- ROMEL PRESIDENT 20000 NULL JOI SALES MANAGER 10000 1000 ... RANDY HEAD ACCOUNTANT 11784 NULL 10 rows selected. SQL> SELECT ename, job, sal, comm 2 FROM emp;
  47. 48. <ul><li>Arithmetic expressions containing a null value evaluate to null. </li></ul>SQL> select ename, 12*sal+comm 2 from emp 3 WHERE ename=‘ROMEL'; ENAME 12*SAL+COMM ---------- ----------- KING NULL
  48. 49. <ul><ul><li>Renames a column heading </li></ul></ul><ul><ul><li>Is useful with calculations </li></ul></ul><ul><ul><li>Immediately follows column name; optional AS keyword between column name and alias </li></ul></ul><ul><ul><li>Requires double quotation marks if it contains spaces or special characters or is case sensitive </li></ul></ul>
  49. 50. SQL> SELECT ename AS name, sal salary 2 FROM emp; NAME SALARY ------------- --------- ... SQL> SELECT ename &quot;Name&quot;, 2 sal*12 &quot;Annual Salary&quot; 3 FROM emp; Name Annual Salary ------------- ------------- ...
  50. 51. <ul><ul><li>Concatenates columns or character strings to other columns </li></ul></ul><ul><ul><li>Is represented by Plus sign (+) </li></ul></ul><ul><ul><li>Creates a resultant column that is a character expression </li></ul></ul>
  51. 52. SQL> SELECT ename + job AS &quot;Employees&quot; 2 FROM emp; Employees ------------------- ROMELPRESIDENT JOISALES MANAGER JANESALES REPRESENTATIVE ANDYSALES REPRESENTATIVE RANDYHEAD ACCOUNTANT ... 10 rows selected.
  52. 53. <ul><ul><li>A literal is a character, a number, or a date included in the SELECT list. </li></ul></ul><ul><ul><li>Date and character literal values must be enclosed within single quotation marks. </li></ul></ul><ul><ul><li>Each character string is output once for each row returned. </li></ul></ul>
  53. 54. Employee Details ------------------------- ... 10 rows selected. SQL> SELECT ename + ' is a ‘ + job 2 AS &quot;Employee Details&quot; 3 FROM emp; ROMEL is a PRESIDENT JOI is a SALES MANAGER JANE is a SALES REPRESENTATIVE ANDY is a SALES REPRESENTATIVE RANDY is a HEAD ACCOUNTANT
  54. 55. <ul><li>The default display of queries is all rows, including duplicate rows. </li></ul>SQL> SELECT deptno 2 FROM emp; DEPTNO --------- 10 30 10 40 ... 14 rows selected.
  55. 56. Eliminate duplicate rows by using the DISTINCT keyword in the SELECT clause. SQL> SELECT DISTINCT deptno 2 FROM emp; DEPTNO --------- 10 30 40
  56. 57. SQL statements <ul><li>SQL </li></ul><ul><ul><li>A language </li></ul></ul><ul><ul><li>ANSI standard </li></ul></ul><ul><ul><li>Keyword cannot be abbreviated </li></ul></ul><ul><ul><li>Statements manipulate data and table definitions in the database </li></ul></ul><ul><li>SQL*Plus </li></ul><ul><ul><li>An environment </li></ul></ul><ul><ul><li>Oracle proprietary </li></ul></ul><ul><ul><li>Keywords can be abbreviated </li></ul></ul><ul><ul><li>Commands do not allow manipulation of values in the database </li></ul></ul>SQL buffer SQL*Plus commands SQL*Plus buffer
  57. 58. &quot;…retrieve all employees in department 30&quot; EMP EMPNO ENAME JOB ... DEPTNO 1001 ROMEL PRESIDENT 40 1002 JOI SALES M... 30 1003 JANE SALES REP 30 . . . 1006 MARK ACCOUN.. 10 ... EMP EMPNO ENAME JOB ... DEPTNO 1002 JOI SALES M... 30 1003 JANE SALES REP 30 . . .
  58. 59. <ul><ul><li>Restrict the rows returned by using the WHERE clause. </li></ul></ul><ul><ul><li>The WHERE clause follows the FROM clause. </li></ul></ul>SELECT [DISTINCT] {*| column [ alias ], ...} FROM table [WHERE condition(s) ];
  59. 60. SQL> SELECT ename, deptno 2 FROM emp 3 WHERE deptno=10; ENAME DEPTNO ---------- --------- --------- RANDY 10 MARK 10 JESSA 10
  60. 61. Operator = > >= < <= <> Meaning Equal to Greater than Greater than or equal to Less than Less than or equal to Not equal to
  61. 62. SQL> SELECT ename,comm 2 FROM emp 3 WHERE comm>=700; ENAME COMM ---------- --------- MARTIN 1000
  62. 63. Operator BETWEEN ...AND... IN(list) LIKE IS NULL Meaning Between two values (inclusive) Match any of a list of values Match a character pattern Is a null value
  63. 64. <ul><li>Use the BETWEEN operator to display rows based on a range of values. </li></ul>ENAME SAL ---------- --------- JOI 10000 JANE 5000 ANDY 5875 MARK 9775 RIZZA 9798 DAVID 6897 SQL> SELECT ename, sal 2 FROM emp 3 WHERE sal BETWEEN 5000 AND 10000; Lower limit Higher limit
  64. 65. <ul><li>Use the IN operator to test for values in a list. </li></ul>SQL> SELECT ename, deptno 2 FROM emp 3 WHERE deptno IN (10, 40); ENAME deptno --------- --------- ROMEL 40 RANDY 10 MARK 10 RIZZA 40
  65. 66. <ul><ul><li>Use the LIKE operator to perform wildcard searches of valid search string values. </li></ul></ul><ul><ul><li>Search conditions can contain either literal characters or numbers. </li></ul></ul><ul><ul><ul><li>% denotes zero or many characters. </li></ul></ul></ul><ul><ul><ul><li>_ denotes one character. </li></ul></ul></ul>SQL> SELECT ename 2 FROM emp 3 WHERE ename LIKE ‘R%';
  66. 67. <ul><ul><li>You can combine pattern-matching characters. </li></ul></ul><ul><ul><li>You can use the ESCAPE identifier to search for &quot;%&quot; or &quot;_&quot;. </li></ul></ul>SQL> SELECT ename 2 FROM emp 3 WHERE ename LIKE '_O%'; ENAME ---------- ROMEL JOI
  67. 68. <ul><li>Test for null values with the IS NULL operator. </li></ul>SQL> SELECT ename, mgr 2 FROM emp 3 WHERE mgr IS NULL; ENAME MGR ---------- --------- ROMEL NULL
  68. 69. Operator AND OR NOT Meaning Returns TRUE if both component conditions are TRUE Returns TRUE if either component condition is TRUE Returns TRUE if the following condition is FALSE

×