NTAUG Introduction in to use of SQL - Part 2 Peter Dominey Copyright © Peter Dominey 2004, Copyright © Peter Dominey 2004,
SQL Review Review
SQL SQL Some Basic SQL Rules Multiple SQL statements are separated by semicolons (;) SQL statement keywords are not case sensitive SQL statement data values are case sensitive All extra white spaces within a SQL statement are ignored. You can add spaces and carriage returns for readability. SQL statements are made up of clauses (e.g., SELECT clause, FROM clause, WHERE clause, etc.)
Common Conventions Used This is the commonly used notation styles and conventions: SELECT [DISTINCT] {* | column-list} FROM table-name Uppercase words = keywords Lowercase words = user supplied data value { } = required items [ ] = optional items | = various choices … = repeating set
SELECT Statement A full SELECT statement consists of 6 clauses: SELECT FROM WHERE GROUP BY HAVING ORDER BY A regular query consists of: SELECT FROM WHERE ORDER BY A grouping (summary) query consists of: SELECT FROM GROUP BY HAVING ORDER BY
Syntactical Diagram of the SELECT Statement ORDER BY ALL * HAVING GROUP BY SELECT FROM WHERE SEARCH CONDITION DISTINCT SELECT-LIST SPECIFY COLUMNS SEARCH CONDITIONS SPECIFY GROUP SORT SPECIFY COLUMN SORT Not all clauses must be used in a query. The minimum that you need for a SELECT statement is SELECT and FROM.
SQL WHERE clause We touched earlier on the where clause We touched earlier on the where clause SELECT job_name FROM job WHERE machine=somename SELECT job_name FROM job WHERE machine=somename And made the use of the simplest form and equals = There are other operators: =Equal <>Not equal >Greater than <Less than >=Greater than or equal <=Less than or equal BETWEEN Between an inclusive range LIKESearch for a pattern
SQL WHERE clause To extract information where you have a list of items: To extract information where you have a list of items: SELECT job_name FROM job WHERE machine IN (machine1,machinezxt,machine1b) SELECT job_name FROM job WHERE machine IN (machine1,machinezxt,machine1b)
SQL Select within Select SELECT job_name FROM job WHERE machine IN (SELECT hostname FROM keymaster) SELECT job_name FROM job WHERE machine IN (SELECT hostname FROM keymaster)
SQL Information from multiple tables Examine the following statement: Examine the following statement: SELECT keymaster.hostid, job.job_name FROM keymaster, job WHERE job.machine=keymaster.hostname SELECT keymaster.hostid, job.job_name FROM keymaster, job WHERE job.machine=keymaster.hostname
Constructing more WHEREs using NOT
Select WHERE & ORDER xql>>[AUTOSYS_SQL_DEV1][autosys] 1> select job_name,owner from job where job_name like 'test%' order by joid ; xql>>[AUTOSYS_SQL_DEV1][autosys] 1> select job_name,owner from job where job_name like 'test%' order by joid ; job_nameowner test_bdsaptc1 autosys test_bdsdbc1 autosys test_bdsaptc1 autosys test_bdsdbc1 autosys
SQL Introduction As stated earlier, this was introduction to SQL and hopefully has provided a taste of the things that can be done to extract information directly from the AutoSys database. As stated earlier, this was introduction to SQL and hopefully has provided a taste of the things that can be done to extract information directly from the AutoSys database