The SELECT clause is used to indicate which expressions to retrieve from the specified data source. Expressions can vary in complexity. Perhaps the least complex expressions are those involving pre-existing attributes. Pre-existing attributes are those attributes already captured within the data source. Other types of expressions include derived attributes, scalar values, and combinations of different types of expressions, among others. It can be difficult to understand the various types of expressions possible within the SELECT clause without concrete examples, so let’s walk through a few.
Before reading further, consider reviewing the Example Data section below to familiarize yourself with the data being used throughout the examples on this page. If you are already familiar with the data, then skip to the Pre-existing Attributes section
Example Data
The schemas, corresponding tables, and data used in the example problems can be found at livesql.oracle.com. To more easily follow the examples covered on this page, consider first reading the table descriptions below and reviewing each table’s contents. To review the data maintained in each table, click on the icon next to the table name. To hide the data, click the icon again.
hr.employees
Contains information related to each employee and a hierarchal employee-manager relationship. There is one row per employee.
| employee_id | first_name | last_name | phone_number | hire_date | job_id | salary | commission_pct | manager_id | department_id | |
|---|---|---|---|---|---|---|---|---|---|---|
| 100 | Steven | King | SKING | 515.123.4567 | 17-Jun-03 | AD_PRES | 24000 | – | – | 90 |
| 101 | Neena | Kochhar | NKOCHHAR | 515.123.4568 | 21-Sep-05 | AD_VP | 17000 | – | 100 | 90 |
| 102 | Lex | De Haan | LDEHAAN | 515.123.4569 | 13-Jan-01 | AD_VP | 17000 | – | 100 | 90 |
| 103 | Alexander | Hunold | AHUNOLD | 590.423.4567 | 3-Jan-06 | IT_PROG | 9000 | – | 102 | 60 |
| 104 | Bruce | Ernst | BERNST | 590.423.4568 | 21-May-07 | IT_PROG | 6000 | – | 103 | 60 |
| 105 | David | Austin | DAUSTIN | 590.423.4569 | 25-Jun-05 | IT_PROG | 4800 | – | 103 | 60 |
| 106 | Valli | Pataballa | VPATABAL | 590.423.4560 | 5-Feb-06 | IT_PROG | 4800 | – | 103 | 60 |
| 107 | Diana | Lorentz | DLORENTZ | 590.423.5567 | 7-Feb-07 | IT_PROG | 4200 | – | 103 | 60 |
| 108 | Nancy | Greenberg | NGREENBE | 515.124.4569 | 17-Aug-02 | FI_MGR | 12008 | – | 101 | 100 |
| 109 | Daniel | Faviet | DFAVIET | 515.124.4169 | 16-Aug-02 | FI_ACCOUNT | 9000 | – | 108 | 100 |
| 110 | John | Chen | JCHEN | 515.124.4269 | 28-Sep-05 | FI_ACCOUNT | 8200 | – | 108 | 100 |
| 111 | Ismael | Sciarra | ISCIARRA | 515.124.4369 | 30-Sep-05 | FI_ACCOUNT | 7700 | – | 108 | 100 |
| 112 | Jose Manuel | Urman | JMURMAN | 515.124.4469 | 7-Mar-06 | FI_ACCOUNT | 7800 | – | 108 | 100 |
| 113 | Luis | Popp | LPOPP | 515.124.4567 | 7-Dec-07 | FI_ACCOUNT | 6900 | – | 108 | 100 |
| 114 | Den | Raphaely | DRAPHEAL | 515.127.4561 | 7-Dec-02 | PU_MAN | 11000 | – | 100 | 30 |
| 115 | Alexander | Khoo | AKHOO | 515.127.4562 | 18-May-03 | PU_CLERK | 3100 | – | 114 | 30 |
| 116 | Shelli | Baida | SBAIDA | 515.127.4563 | 24-Dec-05 | PU_CLERK | 2900 | – | 114 | 30 |
| 117 | Sigal | Tobias | STOBIAS | 515.127.4564 | 24-Jul-05 | PU_CLERK | 2800 | – | 114 | 30 |
| 118 | Guy | Himuro | GHIMURO | 515.127.4565 | 15-Nov-06 | PU_CLERK | 2600 | – | 114 | 30 |
| 119 | Karen | Colmenares | KCOLMENA | 515.127.4566 | 10-Aug-07 | PU_CLERK | 2500 | – | 114 | 30 |
| 120 | Matthew | Weiss | MWEISS | 650.123.1234 | 18-Jul-04 | ST_MAN | 8000 | – | 100 | 50 |
| 121 | Adam | Fripp | AFRIPP | 650.123.2234 | 10-Apr-05 | ST_MAN | 8200 | – | 100 | 50 |
| 122 | Payam | Kaufling | PKAUFLIN | 650.123.3234 | 1-May-03 | ST_MAN | 7900 | – | 100 | 50 |
| 123 | Shanta | Vollman | SVOLLMAN | 650.123.4234 | 10-Oct-05 | ST_MAN | 6500 | – | 100 | 50 |
| 124 | Kevin | Mourgos | KMOURGOS | 650.123.5234 | 16-Nov-07 | ST_MAN | 5800 | – | 100 | 50 |
| 125 | Julia | Nayer | JNAYER | 650.124.1214 | 16-Jul-05 | ST_CLERK | 3200 | – | 120 | 50 |
| 126 | Irene | Mikkilineni | IMIKKILI | 650.124.1224 | 28-Sep-06 | ST_CLERK | 2700 | – | 120 | 50 |
| 127 | James | Landry | JLANDRY | 650.124.1334 | 14-Jan-07 | ST_CLERK | 2400 | – | 120 | 50 |
| 128 | Steven | Markle | SMARKLE | 650.124.1434 | 8-Mar-08 | ST_CLERK | 2200 | – | 120 | 50 |
| 129 | Laura | Bissot | LBISSOT | 650.124.5234 | 20-Aug-05 | ST_CLERK | 3300 | – | 121 | 50 |
| 130 | Mozhe | Atkinson | MATKINSO | 650.124.6234 | 30-Oct-05 | ST_CLERK | 2800 | – | 121 | 50 |
| 131 | James | Marlow | JAMRLOW | 650.124.7234 | 16-Feb-05 | ST_CLERK | 2500 | – | 121 | 50 |
| 132 | TJ | Olson | TJOLSON | 650.124.8234 | 10-Apr-07 | ST_CLERK | 2100 | – | 121 | 50 |
| 133 | Jason | Mallin | JMALLIN | 650.127.1934 | 14-Jun-04 | ST_CLERK | 3300 | – | 122 | 50 |
| 134 | Michael | Rogers | MROGERS | 650.127.1834 | 26-Aug-06 | ST_CLERK | 2900 | – | 122 | 50 |
| 135 | Ki | Gee | KGEE | 650.127.1734 | 12-Dec-07 | ST_CLERK | 2400 | – | 122 | 50 |
| 136 | Hazel | Philtanker | HPHILTAN | 650.127.1634 | 6-Feb-08 | ST_CLERK | 2200 | – | 122 | 50 |
| 137 | Renske | Ladwig | RLADWIG | 650.121.1234 | 14-Jul-03 | ST_CLERK | 3600 | – | 123 | 50 |
| 138 | Stephen | Stiles | SSTILES | 650.121.2034 | 26-Oct-05 | ST_CLERK | 3200 | – | 123 | 50 |
| 139 | John | Seo | JSEO | 650.121.2019 | 12-Feb-06 | ST_CLERK | 2700 | – | 123 | 50 |
| 140 | Joshua | Patel | JPATEL | 650.121.1834 | 6-Apr-06 | ST_CLERK | 2500 | – | 123 | 50 |
| 141 | Trenna | Rajs | TRAJS | 650.121.8009 | 17-Oct-03 | ST_CLERK | 3500 | – | 124 | 50 |
| 142 | Curtis | Davies | CDAVIES | 650.121.2994 | 29-Jan-05 | ST_CLERK | 3100 | – | 124 | 50 |
| 143 | Randall | Matos | RMATOS | 650.121.2874 | 15-Mar-06 | ST_CLERK | 2600 | – | 124 | 50 |
| 144 | Peter | Vargas | PVARGAS | 650.121.2004 | 9-Jul-06 | ST_CLERK | 2500 | – | 124 | 50 |
| 145 | John | Russell | JRUSSEL | 011.44.1344.429268 | 1-Oct-04 | SA_MAN | 14000 | 0.4 | 100 | 80 |
| 146 | Karen | Partners | KPARTNER | 011.44.1344.467268 | 5-Jan-05 | SA_MAN | 13500 | 0.3 | 100 | 80 |
| 147 | Alberto | Errazuriz | AERRAZUR | 011.44.1344.429278 | 10-Mar-05 | SA_MAN | 12000 | 0.3 | 100 | 80 |
| 148 | Gerald | Cambrault | GCAMBRAU | 011.44.1344.619268 | 15-Oct-07 | SA_MAN | 11000 | 0.3 | 100 | 80 |
| 149 | Eleni | Zlotkey | EZLOTKEY | 011.44.1344.429018 | 29-Jan-08 | SA_MAN | 10500 | 0.2 | 100 | 80 |
| 150 | Peter | Tucker | PTUCKER | 011.44.1344.129268 | 30-Jan-05 | SA_REP | 10000 | 0.3 | 145 | 80 |
| 151 | David | Bernstein | DBERNSTE | 011.44.1344.345268 | 24-Mar-05 | SA_REP | 9500 | 0.25 | 145 | 80 |
| 152 | Peter | Hall | PHALL | 011.44.1344.478968 | 20-Aug-05 | SA_REP | 9000 | 0.25 | 145 | 80 |
| 153 | Christopher | Olsen | COLSEN | 011.44.1344.498718 | 30-Mar-06 | SA_REP | 8000 | 0.2 | 145 | 80 |
| 154 | Nanette | Cambrault | NCAMBRAU | 011.44.1344.987668 | 9-Dec-06 | SA_REP | 7500 | 0.2 | 145 | 80 |
| 155 | Oliver | Tuvault | OTUVAULT | 011.44.1344.486508 | 23-Nov-07 | SA_REP | 7000 | 0.15 | 145 | 80 |
| 156 | Janette | King | JKING | 011.44.1345.429268 | 30-Jan-04 | SA_REP | 10000 | 0.35 | 146 | 80 |
| 157 | Patrick | Sully | PSULLY | 011.44.1345.929268 | 4-Mar-04 | SA_REP | 9500 | 0.35 | 146 | 80 |
| 158 | Allan | McEwen | AMCEWEN | 011.44.1345.829268 | 1-Aug-04 | SA_REP | 9000 | 0.35 | 146 | 80 |
| 159 | Lindsey | Smith | LSMITH | 011.44.1345.729268 | 10-Mar-05 | SA_REP | 8000 | 0.3 | 146 | 80 |
| 160 | Louise | Doran | LDORAN | 011.44.1345.629268 | 15-Dec-05 | SA_REP | 7500 | 0.3 | 146 | 80 |
| 161 | Sarath | Sewall | SSEWALL | 011.44.1345.529268 | 3-Nov-06 | SA_REP | 7000 | 0.25 | 146 | 80 |
| 162 | Clara | Vishney | CVISHNEY | 011.44.1346.129268 | 11-Nov-05 | SA_REP | 10500 | 0.25 | 147 | 80 |
| 163 | Danielle | Greene | DGREENE | 011.44.1346.229268 | 19-Mar-07 | SA_REP | 9500 | 0.15 | 147 | 80 |
| 164 | Mattea | Marvins | MMARVINS | 011.44.1346.329268 | 24-Jan-08 | SA_REP | 7200 | 0.1 | 147 | 80 |
| 165 | David | Lee | DLEE | 011.44.1346.529268 | 23-Feb-08 | SA_REP | 6800 | 0.1 | 147 | 80 |
| 166 | Sundar | Ande | SANDE | 011.44.1346.629268 | 24-Mar-08 | SA_REP | 6400 | 0.1 | 147 | 80 |
| 167 | Amit | Banda | ABANDA | 011.44.1346.729268 | 21-Apr-08 | SA_REP | 6200 | 0.1 | 147 | 80 |
| 168 | Lisa | Ozer | LOZER | 011.44.1343.929268 | 11-Mar-05 | SA_REP | 11500 | 0.25 | 148 | 80 |
| 169 | Harrison | Bloom | HBLOOM | 011.44.1343.829268 | 23-Mar-06 | SA_REP | 10000 | 0.2 | 148 | 80 |
| 170 | Tayler | Fox | TFOX | 011.44.1343.729268 | 24-Jan-06 | SA_REP | 9600 | 0.2 | 148 | 80 |
| 171 | William | Smith | WSMITH | 011.44.1343.629268 | 23-Feb-07 | SA_REP | 7400 | 0.15 | 148 | 80 |
| 172 | Elizabeth | Bates | EBATES | 011.44.1343.529268 | 24-Mar-07 | SA_REP | 7300 | 0.15 | 148 | 80 |
| 173 | Sundita | Kumar | SKUMAR | 011.44.1343.329268 | 21-Apr-08 | SA_REP | 6100 | 0.1 | 148 | 80 |
| 174 | Ellen | Abel | EABEL | 011.44.1644.429267 | 11-May-04 | SA_REP | 11000 | 0.3 | 149 | 80 |
| 175 | Alyssa | Hutton | AHUTTON | 011.44.1644.429266 | 19-Mar-05 | SA_REP | 8800 | 0.25 | 149 | 80 |
| 176 | Jonathon | Taylor | JTAYLOR | 011.44.1644.429265 | 24-Mar-06 | SA_REP | 8600 | 0.2 | 149 | 80 |
| 177 | Jack | Livingston | JLIVINGS | 011.44.1644.429264 | 23-Apr-06 | SA_REP | 8400 | 0.2 | 149 | 80 |
| 178 | Kimberely | Grant | KGRANT | 011.44.1644.429263 | 24-May-07 | SA_REP | 7000 | 0.15 | 149 | – |
| 179 | Charles | Johnson | CJOHNSON | 011.44.1644.429262 | 4-Jan-08 | SA_REP | 6200 | 0.1 | 149 | 80 |
| 180 | Winston | Taylor | WTAYLOR | 650.507.9876 | 24-Jan-06 | SH_CLERK | 3200 | – | 120 | 50 |
| 181 | Jean | Fleaur | JFLEAUR | 650.507.9877 | 23-Feb-06 | SH_CLERK | 3100 | – | 120 | 50 |
| 182 | Martha | Sullivan | MSULLIVA | 650.507.9878 | 21-Jun-07 | SH_CLERK | 2500 | – | 120 | 50 |
| 183 | Girard | Geoni | GGEONI | 650.507.9879 | 3-Feb-08 | SH_CLERK | 2800 | – | 120 | 50 |
| 184 | Nandita | Sarchand | NSARCHAN | 650.509.1876 | 27-Jan-04 | SH_CLERK | 4200 | – | 121 | 50 |
| 185 | Alexis | Bull | ABULL | 650.509.2876 | 20-Feb-05 | SH_CLERK | 4100 | – | 121 | 50 |
| 186 | Julia | Dellinger | JDELLING | 650.509.3876 | 24-Jun-06 | SH_CLERK | 3400 | – | 121 | 50 |
| 187 | Anthony | Cabrio | ACABRIO | 650.509.4876 | 7-Feb-07 | SH_CLERK | 3000 | – | 121 | 50 |
| 188 | Kelly | Chung | KCHUNG | 650.505.1876 | 14-Jun-05 | SH_CLERK | 3800 | – | 122 | 50 |
| 189 | Jennifer | Dilly | JDILLY | 650.505.2876 | 13-Aug-05 | SH_CLERK | 3600 | – | 122 | 50 |
| 190 | Timothy | Gates | TGATES | 650.505.3876 | 11-Jul-06 | SH_CLERK | 2900 | – | 122 | 50 |
| 191 | Randall | Perkins | RPERKINS | 650.505.4876 | 19-Dec-07 | SH_CLERK | 2500 | – | 122 | 50 |
| 192 | Sarah | Bell | SBELL | 650.501.1876 | 4-Feb-04 | SH_CLERK | 4000 | – | 123 | 50 |
| 193 | Britney | Everett | BEVERETT | 650.501.2876 | 3-Mar-05 | SH_CLERK | 3900 | – | 123 | 50 |
| 194 | Samuel | McCain | SMCCAIN | 650.501.3876 | 1-Jul-06 | SH_CLERK | 3200 | – | 123 | 50 |
| 195 | Vance | Jones | VJONES | 650.501.4876 | 17-Mar-07 | SH_CLERK | 2800 | – | 123 | 50 |
| 196 | Alana | Walsh | AWALSH | 650.507.9811 | 24-Apr-06 | SH_CLERK | 3100 | – | 124 | 50 |
| 197 | Kevin | Feeney | KFEENEY | 650.507.9822 | 23-May-06 | SH_CLERK | 3000 | – | 124 | 50 |
| 198 | Donald | OConnell | DOCONNEL | 650.507.9833 | 21-Jun-07 | SH_CLERK | 2600 | – | 124 | 50 |
| 199 | Douglas | Grant | DGRANT | 650.507.9844 | 13-Jan-08 | SH_CLERK | 2600 | – | 124 | 50 |
| 200 | Jennifer | Whalen | JWHALEN | 515.123.4444 | 17-Sep-03 | AD_ASST | 4400 | – | 101 | 10 |
| 201 | Michael | Hartstein | MHARTSTE | 515.123.5555 | 17-Feb-04 | MK_MAN | 13000 | – | 100 | 20 |
| 202 | Pat | Fay | PFAY | 603.123.6666 | 17-Aug-05 | MK_REP | 6000 | – | 201 | 20 |
| 203 | Susan | Mavris | SMAVRIS | 515.123.7777 | 7-Jun-02 | HR_REP | 6500 | – | 101 | 40 |
| 204 | Hermann | Baer | HBAER | 515.123.8888 | 7-Jun-02 | PR_REP | 10000 | – | 101 | 70 |
| 205 | Shelley | Higgins | SHIGGINS | 515.123.8080 | 7-Jun-02 | AC_MGR | 12008 | – | 101 | 110 |
| 206 | William | Gietz | WGIETZ | 515.123.8181 | 7-Jun-02 | AC_ACCOUNT | 8300 | – | 205 | 110 |
hr.jobs
Contains information related to each job. It’s possible for a job to exist without being assigned to any employee. There is one row per job.
| job_id | job_title | min_salary | max_salary |
|---|---|---|---|
| AD_PRES | President | 20080 | 40000 |
| AD_VP | Administration Vice President | 15000 | 30000 |
| AD_ASST | Administration Assistant | 3000 | 6000 |
| FI_MGR | Finance Manager | 8200 | 16000 |
| FI_ACCOUNT | Accountant | 4200 | 9000 |
| AC_MGR | Accounting Manager | 8200 | 16000 |
| AC_ACCOUNT | Public Accountant | 4200 | 9000 |
| SA_MAN | Sales Manager | 10000 | 20080 |
| SA_REP | Sales Representative | 6000 | 12008 |
| PU_MAN | Purchasing Manager | 8000 | 15000 |
| PU_CLERK | Purchasing Clerk | 2500 | 5500 |
| ST_MAN | Stock Manager | 5500 | 8500 |
| ST_CLERK | Stock Clerk | 2008 | 5000 |
| SH_CLERK | Shipping Clerk | 2500 | 5500 |
| IT_PROG | Programmer | 4000 | 10000 |
| MK_MAN | Marketing Manager | 9000 | 15000 |
| MK_REP | Marketing Representative | 4000 | 9000 |
| HR_REP | Human Resources Representative | 4000 | 9000 |
| PR_REP | Public Relations Representative | 4500 | 10500 |
Pre-existing Attributes
For the first example, let’s consider a scenario where the task is to generate a report containing the ID, first name, last name, and hire date of each employee. Based on the employees table above, each of these attributes are part of the table’s definition and are already being captured. As a result, we simply need to list those attributes within the SELECT clause and specify the data source or object containing the attributes of interest using the FROM clause. For the examples on this page, it is sufficient to simply know the FROM clause follows the expression list and is used to specify the data source. The code required to fulfill the task and the corresponding output can be found below.
SELECT
hr.employees.employee_id,
hr.employees.first_name,
hr.employees.last_name,
hr.employees.hire_date
FROM
hr.employees;
| employee_id | first_name | last_name | hire_date |
|---|---|---|---|
| 100 | Steven | King | 17-JUN-03 |
| 101 | Neena | Kochhar | 21-SEP-05 |
| 102 | Lex | De Haan | 13-JAN-01 |
| 103 | Alexander | Hunold | 03-JAN-06 |
| 104 | Bruce | Ernst | 21-MAY-07 |
| 105 | David | Austin | 25-JUN-05 |
| 106 | Valli | Pataballa | 05-FEB-06 |
| 107 | Diana | Lorentz | 07-FEB-07 |
| 108 | Nancy | Greenberg | 17-AUG-02 |
| 109 | Daniel | Faviet | 16-AUG-02 |
| 110 | John | Chen | 28-SEP-05 |
| 111 | Ismael | Sciarra | 30-SEP-05 |
| 112 | Jose Manuel | Urman | 07-MAR-06 |
| 113 | Luis | Popp | 07-DEC-07 |
| 114 | Den | Raphaely | 07-DEC-02 |
| 115 | Alexander | Khoo | 18-MAY-03 |
| 116 | Shelli | Baida | 24-DEC-05 |
| 117 | Sigal | Tobias | 24-JUL-05 |
| 118 | Guy | Himuro | 15-NOV-06 |
| 119 | Karen | Colmenares | 10-AUG-07 |
| 120 | Matthew | Weiss | 18-JUL-04 |
| 121 | Adam | Fripp | 10-APR-05 |
| 122 | Payam | Kaufling | 01-MAY-03 |
| 123 | Shanta | Vollman | 10-OCT-05 |
| 124 | Kevin | Mourgos | 16-NOV-07 |
| 125 | Julia | Nayer | 16-JUL-05 |
| 126 | Irene | Mikkilineni | 28-SEP-06 |
| 127 | James | Landry | 14-JAN-07 |
| 128 | Steven | Markle | 08-MAR-08 |
| 129 | Laura | Bissot | 20-AUG-05 |
| 130 | Mozhe | Atkinson | 30-OCT-05 |
| 131 | James | Marlow | 16-FEB-05 |
| 132 | TJ | Olson | 10-APR-07 |
| 133 | Jason | Mallin | 14-JUN-04 |
| 134 | Michael | Rogers | 26-AUG-06 |
| 135 | Ki | Gee | 12-DEC-07 |
| 136 | Hazel | Philtanker | 06-FEB-08 |
| 137 | Renske | Ladwig | 14-JUL-03 |
| 138 | Stephen | Stiles | 26-OCT-05 |
| 139 | John | Seo | 12-FEB-06 |
| 140 | Joshua | Patel | 06-APR-06 |
| 141 | Trenna | Rajs | 17-OCT-03 |
| 142 | Curtis | Davies | 29-JAN-05 |
| 143 | Randall | Matos | 15-MAR-06 |
| 144 | Peter | Vargas | 09-JUL-06 |
| 145 | John | Russell | 01-OCT-04 |
| 146 | Karen | Partners | 05-JAN-05 |
| 147 | Alberto | Errazuriz | 10-MAR-05 |
| 148 | Gerald | Cambrault | 15-OCT-07 |
| 149 | Eleni | Zlotkey | 29-JAN-08 |
| 150 | Peter | Tucker | 30-JAN-05 |
| 151 | David | Bernstein | 24-MAR-05 |
| 152 | Peter | Hall | 20-AUG-05 |
| 153 | Christopher | Olsen | 30-MAR-06 |
| 154 | Nanette | Cambrault | 09-DEC-06 |
| 155 | Oliver | Tuvault | 23-NOV-07 |
| 156 | Janette | King | 30-JAN-04 |
| 157 | Patrick | Sully | 04-MAR-04 |
| 158 | Allan | McEwen | 01-AUG-04 |
| 159 | Lindsey | Smith | 10-MAR-05 |
| 160 | Louise | Doran | 15-DEC-05 |
| 161 | Sarath | Sewall | 03-NOV-06 |
| 162 | Clara | Vishney | 11-NOV-05 |
| 163 | Danielle | Greene | 19-MAR-07 |
| 164 | Mattea | Marvins | 24-JAN-08 |
| 165 | David | Lee | 23-FEB-08 |
| 166 | Sundar | Ande | 24-MAR-08 |
| 167 | Amit | Banda | 21-APR-08 |
| 168 | Lisa | Ozer | 11-MAR-05 |
| 169 | Harrison | Bloom | 23-MAR-06 |
| 170 | Tayler | Fox | 24-JAN-06 |
| 171 | William | Smith | 23-FEB-07 |
| 172 | Elizabeth | Bates | 24-MAR-07 |
| 173 | Sundita | Kumar | 21-APR-08 |
| 174 | Ellen | Abel | 11-MAY-04 |
| 175 | Alyssa | Hutton | 19-MAR-05 |
| 176 | Jonathon | Taylor | 24-MAR-06 |
| 177 | Jack | Livingston | 23-APR-06 |
| 178 | Kimberely | Grant | 24-MAY-07 |
| 179 | Charles | Johnson | 04-JAN-08 |
| 180 | Winston | Taylor | 24-JAN-06 |
| 181 | Jean | Fleaur | 23-FEB-06 |
| 182 | Martha | Sullivan | 21-JUN-07 |
| 183 | Girard | Geoni | 03-FEB-08 |
| 184 | Nandita | Sarchand | 27-JAN-04 |
| 185 | Alexis | Bull | 20-FEB-05 |
| 186 | Julia | Dellinger | 24-JUN-06 |
| 187 | Anthony | Cabrio | 07-FEB-07 |
| 188 | Kelly | Chung | 14-JUN-05 |
| 189 | Jennifer | Dilly | 13-AUG-05 |
| 190 | Timothy | Gates | 11-JUL-06 |
| 191 | Randall | Perkins | 19-DEC-07 |
| 192 | Sarah | Bell | 04-FEB-04 |
| 193 | Britney | Everett | 03-MAR-05 |
| 194 | Samuel | McCain | 01-JUL-06 |
| 195 | Vance | Jones | 17-MAR-07 |
| 196 | Alana | Walsh | 24-APR-06 |
| 197 | Kevin | Feeney | 23-MAY-06 |
| 198 | Donald | OConnell | 21-JUN-07 |
| 199 | Douglas | Grant | 13-JAN-08 |
| 200 | Jennifer | Whalen | 17-SEP-03 |
| 201 | Michael | Hartstein | 17-FEB-04 |
| 202 | Pat | Fay | 17-AUG-05 |
| 203 | Susan | Mavris | 07-JUN-02 |
| 204 | Hermann | Baer | 07-JUN-02 |
| 205 | Shelley | Higgins | 07-JUN-02 |
| 206 | William | Gietz | 07-JUN-02 |
Notice the commas present in the code above. When there is more than one expression, it is required to separate expressions with commas, leaving off the trailing comma. In the beginning of your SQL learning journey, it may be helpful to remember each SELECT clause requires one less comma than the number of expressions specified (i.e., n-1).
Derived Attributes
Derived attributes bridge the gap between the attributes stored in the data source and the attributes we need at any given time. Suppose, we are tasked with generating a report for people managers reminding them of important dates and milestones for each employee, such as the following:
– Benefits eligibility date (i.e., 30 days following the hire date)
– Ninety day review (i.e., 90 days following the hire date)
– One-year anniversary
– Five-year anniversary
These data points are not being captured, so we’ll need to derive them at run-time. To do so, we’ll take advantage of Oracle’s built-in function, ADD_MONTHS, and straightforward means to perform date arithmetic. The code required to fulfill the task and the corresponding output can be found below. To learn more about Oracle’s built-in datetime functions and date arithmetic through real-world examples, see my datetime functions page.
SELECT
hr.employees.employee_id,
hr.employees.hire_date,
hr.employees.hire_date + 30 AS benefits_eligible_date,
hr.employees.hire_date + 90 AS ninety_day_review,
ADD_MONTHS(hr.employees.hire_date, 12) AS one_year_anniversary,
ADD_MONTHS(hr.employees.hire_date, 60) AS five_year_anniversay
FROM
hr.employees;
| employee_id | hire_date | benefits_eligible_date | ninety_day_review | one_year_anniversary | five_year_anniversary |
|---|---|---|---|---|---|
| 100 | 17-JUN-03 | 17-JUL-03 | 15-SEP-03 | 17-JUN-04 | 17-JUN-08 |
| 101 | 21-SEP-05 | 21-OCT-05 | 20-DEC-05 | 21-SEP-06 | 21-SEP-10 |
| 102 | 13-JAN-01 | 12-FEB-01 | 13-APR-01 | 13-JAN-02 | 13-JAN-06 |
| 103 | 03-JAN-06 | 02-FEB-06 | 03-APR-06 | 03-JAN-07 | 03-JAN-11 |
| 104 | 21-MAY-07 | 20-JUN-07 | 19-AUG-07 | 21-MAY-08 | 21-MAY-12 |
| 105 | 25-JUN-05 | 25-JUL-05 | 23-SEP-05 | 25-JUN-06 | 25-JUN-10 |
| 106 | 05-FEB-06 | 07-MAR-06 | 06-MAY-06 | 05-FEB-07 | 05-FEB-11 |
| 107 | 07-FEB-07 | 09-MAR-07 | 08-MAY-07 | 07-FEB-08 | 07-FEB-12 |
| 108 | 17-AUG-02 | 16-SEP-02 | 15-NOV-02 | 17-AUG-03 | 17-AUG-07 |
| 109 | 16-AUG-02 | 15-SEP-02 | 14-NOV-02 | 16-AUG-03 | 16-AUG-07 |
| 110 | 28-SEP-05 | 28-OCT-05 | 27-DEC-05 | 28-SEP-06 | 28-SEP-10 |
| 111 | 30-SEP-05 | 30-OCT-05 | 29-DEC-05 | 30-SEP-06 | 30-SEP-10 |
| 112 | 07-MAR-06 | 06-APR-06 | 05-JUN-06 | 07-MAR-07 | 07-MAR-11 |
| 113 | 07-DEC-07 | 06-JAN-08 | 06-MAR-08 | 07-DEC-08 | 07-DEC-12 |
| 114 | 07-DEC-02 | 06-JAN-03 | 07-MAR-03 | 07-DEC-03 | 07-DEC-07 |
| 115 | 18-MAY-03 | 17-JUN-03 | 16-AUG-03 | 18-MAY-04 | 18-MAY-08 |
| 116 | 24-DEC-05 | 23-JAN-06 | 24-MAR-06 | 24-DEC-06 | 24-DEC-10 |
| 117 | 24-JUL-05 | 23-AUG-05 | 22-OCT-05 | 24-JUL-06 | 24-JUL-10 |
| 118 | 15-NOV-06 | 15-DEC-06 | 13-FEB-07 | 15-NOV-07 | 15-NOV-11 |
| 119 | 10-AUG-07 | 09-SEP-07 | 08-NOV-07 | 10-AUG-08 | 10-AUG-12 |
| 120 | 18-JUL-04 | 17-AUG-04 | 16-OCT-04 | 18-JUL-05 | 18-JUL-09 |
| 121 | 10-APR-05 | 10-MAY-05 | 09-JUL-05 | 10-APR-06 | 10-APR-10 |
| 122 | 01-MAY-03 | 31-MAY-03 | 30-JUL-03 | 01-MAY-04 | 01-MAY-08 |
| 123 | 10-OCT-05 | 09-NOV-05 | 08-JAN-06 | 10-OCT-06 | 10-OCT-10 |
| 124 | 16-NOV-07 | 16-DEC-07 | 14-FEB-08 | 16-NOV-08 | 16-NOV-12 |
| 125 | 16-JUL-05 | 15-AUG-05 | 14-OCT-05 | 16-JUL-06 | 16-JUL-10 |
| 126 | 28-SEP-06 | 28-OCT-06 | 27-DEC-06 | 28-SEP-07 | 28-SEP-11 |
| 127 | 14-JAN-07 | 13-FEB-07 | 14-APR-07 | 14-JAN-08 | 14-JAN-12 |
| 128 | 08-MAR-08 | 07-APR-08 | 06-JUN-08 | 08-MAR-09 | 08-MAR-13 |
| 129 | 20-AUG-05 | 19-SEP-05 | 18-NOV-05 | 20-AUG-06 | 20-AUG-10 |
| 130 | 30-OCT-05 | 29-NOV-05 | 28-JAN-06 | 30-OCT-06 | 30-OCT-10 |
| 131 | 16-FEB-05 | 18-MAR-05 | 17-MAY-05 | 16-FEB-06 | 16-FEB-10 |
| 132 | 10-APR-07 | 10-MAY-07 | 09-JUL-07 | 10-APR-08 | 10-APR-12 |
| 133 | 14-JUN-04 | 14-JUL-04 | 12-SEP-04 | 14-JUN-05 | 14-JUN-09 |
| 134 | 26-AUG-06 | 25-SEP-06 | 24-NOV-06 | 26-AUG-07 | 26-AUG-11 |
| 135 | 12-DEC-07 | 11-JAN-08 | 11-MAR-08 | 12-DEC-08 | 12-DEC-12 |
| 136 | 06-FEB-08 | 07-MAR-08 | 06-MAY-08 | 06-FEB-09 | 06-FEB-13 |
| 137 | 14-JUL-03 | 13-AUG-03 | 12-OCT-03 | 14-JUL-04 | 14-JUL-08 |
| 138 | 26-OCT-05 | 25-NOV-05 | 24-JAN-06 | 26-OCT-06 | 26-OCT-10 |
| 139 | 12-FEB-06 | 14-MAR-06 | 13-MAY-06 | 12-FEB-07 | 12-FEB-11 |
| 140 | 06-APR-06 | 06-MAY-06 | 05-JUL-06 | 06-APR-07 | 06-APR-11 |
| 141 | 17-OCT-03 | 16-NOV-03 | 15-JAN-04 | 17-OCT-04 | 17-OCT-08 |
| 142 | 29-JAN-05 | 28-FEB-05 | 29-APR-05 | 29-JAN-06 | 29-JAN-10 |
| 143 | 15-MAR-06 | 14-APR-06 | 13-JUN-06 | 15-MAR-07 | 15-MAR-11 |
| 144 | 09-JUL-06 | 08-AUG-06 | 07-OCT-06 | 09-JUL-07 | 09-JUL-11 |
| 145 | 01-OCT-04 | 31-OCT-04 | 30-DEC-04 | 01-OCT-05 | 01-OCT-09 |
| 146 | 05-JAN-05 | 04-FEB-05 | 05-APR-05 | 05-JAN-06 | 05-JAN-10 |
| 147 | 10-MAR-05 | 09-APR-05 | 08-JUN-05 | 10-MAR-06 | 10-MAR-10 |
| 148 | 15-OCT-07 | 14-NOV-07 | 13-JAN-08 | 15-OCT-08 | 15-OCT-12 |
| 149 | 29-JAN-08 | 28-FEB-08 | 28-APR-08 | 29-JAN-09 | 29-JAN-13 |
| 150 | 30-JAN-05 | 01-MAR-05 | 30-APR-05 | 30-JAN-06 | 30-JAN-10 |
| 151 | 24-MAR-05 | 23-APR-05 | 22-JUN-05 | 24-MAR-06 | 24-MAR-10 |
| 152 | 20-AUG-05 | 19-SEP-05 | 18-NOV-05 | 20-AUG-06 | 20-AUG-10 |
| 153 | 30-MAR-06 | 29-APR-06 | 28-JUN-06 | 30-MAR-07 | 30-MAR-11 |
| 154 | 09-DEC-06 | 08-JAN-07 | 09-MAR-07 | 09-DEC-07 | 09-DEC-11 |
| 155 | 23-NOV-07 | 23-DEC-07 | 21-FEB-08 | 23-NOV-08 | 23-NOV-12 |
| 156 | 30-JAN-04 | 29-FEB-04 | 29-APR-04 | 30-JAN-05 | 30-JAN-09 |
| 157 | 04-MAR-04 | 03-APR-04 | 02-JUN-04 | 04-MAR-05 | 04-MAR-09 |
| 158 | 01-AUG-04 | 31-AUG-04 | 30-OCT-04 | 01-AUG-05 | 01-AUG-09 |
| 159 | 10-MAR-05 | 09-APR-05 | 08-JUN-05 | 10-MAR-06 | 10-MAR-10 |
| 160 | 15-DEC-05 | 14-JAN-06 | 15-MAR-06 | 15-DEC-06 | 15-DEC-10 |
| 161 | 03-NOV-06 | 03-DEC-06 | 01-FEB-07 | 03-NOV-07 | 03-NOV-11 |
| 162 | 11-NOV-05 | 11-DEC-05 | 09-FEB-06 | 11-NOV-06 | 11-NOV-10 |
| 163 | 19-MAR-07 | 18-APR-07 | 17-JUN-07 | 19-MAR-08 | 19-MAR-12 |
| 164 | 24-JAN-08 | 23-FEB-08 | 23-APR-08 | 24-JAN-09 | 24-JAN-13 |
| 165 | 23-FEB-08 | 24-MAR-08 | 23-MAY-08 | 23-FEB-09 | 23-FEB-13 |
| 166 | 24-MAR-08 | 23-APR-08 | 22-JUN-08 | 24-MAR-09 | 24-MAR-13 |
| 167 | 21-APR-08 | 21-MAY-08 | 20-JUL-08 | 21-APR-09 | 21-APR-13 |
| 168 | 11-MAR-05 | 10-APR-05 | 09-JUN-05 | 11-MAR-06 | 11-MAR-10 |
| 169 | 23-MAR-06 | 22-APR-06 | 21-JUN-06 | 23-MAR-07 | 23-MAR-11 |
| 170 | 24-JAN-06 | 23-FEB-06 | 24-APR-06 | 24-JAN-07 | 24-JAN-11 |
| 171 | 23-FEB-07 | 25-MAR-07 | 24-MAY-07 | 23-FEB-08 | 23-FEB-12 |
| 172 | 24-MAR-07 | 23-APR-07 | 22-JUN-07 | 24-MAR-08 | 24-MAR-12 |
| 173 | 21-APR-08 | 21-MAY-08 | 20-JUL-08 | 21-APR-09 | 21-APR-13 |
| 174 | 11-MAY-04 | 10-JUN-04 | 09-AUG-04 | 11-MAY-05 | 11-MAY-09 |
| 175 | 19-MAR-05 | 18-APR-05 | 17-JUN-05 | 19-MAR-06 | 19-MAR-10 |
| 176 | 24-MAR-06 | 23-APR-06 | 22-JUN-06 | 24-MAR-07 | 24-MAR-11 |
| 177 | 23-APR-06 | 23-MAY-06 | 22-JUL-06 | 23-APR-07 | 23-APR-11 |
| 178 | 24-MAY-07 | 23-JUN-07 | 22-AUG-07 | 24-MAY-08 | 24-MAY-12 |
| 179 | 04-JAN-08 | 03-FEB-08 | 03-APR-08 | 04-JAN-09 | 04-JAN-13 |
| 180 | 24-JAN-06 | 23-FEB-06 | 24-APR-06 | 24-JAN-07 | 24-JAN-11 |
| 181 | 23-FEB-06 | 25-MAR-06 | 24-MAY-06 | 23-FEB-07 | 23-FEB-11 |
| 182 | 21-JUN-07 | 21-JUL-07 | 19-SEP-07 | 21-JUN-08 | 21-JUN-12 |
| 183 | 03-FEB-08 | 04-MAR-08 | 03-MAY-08 | 03-FEB-09 | 03-FEB-13 |
| 184 | 27-JAN-04 | 26-FEB-04 | 26-APR-04 | 27-JAN-05 | 27-JAN-09 |
| 185 | 20-FEB-05 | 22-MAR-05 | 21-MAY-05 | 20-FEB-06 | 20-FEB-10 |
| 186 | 24-JUN-06 | 24-JUL-06 | 22-SEP-06 | 24-JUN-07 | 24-JUN-11 |
| 187 | 07-FEB-07 | 09-MAR-07 | 08-MAY-07 | 07-FEB-08 | 07-FEB-12 |
| 188 | 14-JUN-05 | 14-JUL-05 | 12-SEP-05 | 14-JUN-06 | 14-JUN-10 |
| 189 | 13-AUG-05 | 12-SEP-05 | 11-NOV-05 | 13-AUG-06 | 13-AUG-10 |
| 190 | 11-JUL-06 | 10-AUG-06 | 09-OCT-06 | 11-JUL-07 | 11-JUL-11 |
| 191 | 19-DEC-07 | 18-JAN-08 | 18-MAR-08 | 19-DEC-08 | 19-DEC-12 |
| 192 | 04-FEB-04 | 05-MAR-04 | 04-MAY-04 | 04-FEB-05 | 04-FEB-09 |
| 193 | 03-MAR-05 | 02-APR-05 | 01-JUN-05 | 03-MAR-06 | 03-MAR-10 |
| 194 | 01-JUL-06 | 31-JUL-06 | 29-SEP-06 | 01-JUL-07 | 01-JUL-11 |
| 195 | 17-MAR-07 | 16-APR-07 | 15-JUN-07 | 17-MAR-08 | 17-MAR-12 |
| 196 | 24-APR-06 | 24-MAY-06 | 23-JUL-06 | 24-APR-07 | 24-APR-11 |
| 197 | 23-MAY-06 | 22-JUN-06 | 21-AUG-06 | 23-MAY-07 | 23-MAY-11 |
| 198 | 21-JUN-07 | 21-JUL-07 | 19-SEP-07 | 21-JUN-08 | 21-JUN-12 |
| 199 | 13-JAN-08 | 12-FEB-08 | 12-APR-08 | 13-JAN-09 | 13-JAN-13 |
| 200 | 17-SEP-03 | 17-OCT-03 | 16-DEC-03 | 17-SEP-04 | 17-SEP-08 |
| 201 | 17-FEB-04 | 18-MAR-04 | 17-MAY-04 | 17-FEB-05 | 17-FEB-09 |
| 202 | 17-AUG-05 | 16-SEP-05 | 15-NOV-05 | 17-AUG-06 | 17-AUG-10 |
| 203 | 07-JUN-02 | 07-JUL-02 | 05-SEP-02 | 07-JUN-03 | 07-JUN-07 |
| 204 | 07-JUN-02 | 07-JUL-02 | 05-SEP-02 | 07-JUN-03 | 07-JUN-07 |
| 205 | 07-JUN-02 | 07-JUL-02 | 05-SEP-02 | 07-JUN-03 | 07-JUN-07 |
| 206 | 07-JUN-02 | 07-JUL-02 | 05-SEP-02 | 07-JUN-03 | 07-JUN-07 |
Scalar Values
A task’s requirements may require you to incorporate scalar values (e.g., 1, 1.10, 100, etc.) into your solution. For example, suppose human resources would like you to generate a report displaying each employee’s salary. Additionally, they would like to see employees’ salaries with a three percent increase applied. To achieve this, simply multiply the pre-existing attribute, salary, by 1.03 using the ‘*’ symbol. For clarity, see the code and corresponding output below.
SELECT
hr.employees.employee_id,
hr.employees.salary,
hr.employees.salary * 1.03 AS new_salary
FROM
hr.employees;
| employee_id | salary | new_salary |
|---|---|---|
| 100 | 24000 | 24720 |
| 101 | 17000 | 17510 |
| 102 | 17000 | 17510 |
| 103 | 9000 | 9270 |
| 104 | 6000 | 6180 |
| 105 | 4800 | 4944 |
| 106 | 4800 | 4944 |
| 107 | 4200 | 4326 |
| 108 | 12008 | 12368.24 |
| 109 | 9000 | 9270 |
| 110 | 8200 | 8446 |
| 111 | 7700 | 7931 |
| 112 | 7800 | 8034 |
| 113 | 6900 | 7107 |
| 114 | 11000 | 11330 |
| 115 | 3100 | 3193 |
| 116 | 2900 | 2987 |
| 117 | 2800 | 2884 |
| 118 | 2600 | 2678 |
| 119 | 2500 | 2575 |
| 120 | 8000 | 8240 |
| 121 | 8200 | 8446 |
| 122 | 7900 | 8137 |
| 123 | 6500 | 6695 |
| 124 | 5800 | 5974 |
| 125 | 3200 | 3296 |
| 126 | 2700 | 2781 |
| 127 | 2400 | 2472 |
| 128 | 2200 | 2266 |
| 129 | 3300 | 3399 |
| 130 | 2800 | 2884 |
| 131 | 2500 | 2575 |
| 132 | 2100 | 2163 |
| 133 | 3300 | 3399 |
| 134 | 2900 | 2987 |
| 135 | 2400 | 2472 |
| 136 | 2200 | 2266 |
| 137 | 3600 | 3708 |
| 138 | 3200 | 3296 |
| 139 | 2700 | 2781 |
| 140 | 2500 | 2575 |
| 141 | 3500 | 3605 |
| 142 | 3100 | 3193 |
| 143 | 2600 | 2678 |
| 144 | 2500 | 2575 |
| 145 | 14000 | 14420 |
| 146 | 13500 | 13905 |
| 147 | 12000 | 12360 |
| 148 | 11000 | 11330 |
| 149 | 10500 | 10815 |
| 150 | 10000 | 10300 |
| 151 | 9500 | 9785 |
| 152 | 9000 | 9270 |
| 153 | 8000 | 8240 |
| 154 | 7500 | 7725 |
| 155 | 7000 | 7210 |
| 156 | 10000 | 10300 |
| 157 | 9500 | 9785 |
| 158 | 9000 | 9270 |
| 159 | 8000 | 8240 |
| 160 | 7500 | 7725 |
| 161 | 7000 | 7210 |
| 162 | 10500 | 10815 |
| 163 | 9500 | 9785 |
| 164 | 7200 | 7416 |
| 165 | 6800 | 7004 |
| 166 | 6400 | 6592 |
| 167 | 6200 | 6386 |
| 168 | 11500 | 11845 |
| 169 | 10000 | 10300 |
| 170 | 9600 | 9888 |
| 171 | 7400 | 7622 |
| 172 | 7300 | 7519 |
| 173 | 6100 | 6283 |
| 174 | 11000 | 11330 |
| 175 | 8800 | 9064 |
| 176 | 8600 | 8858 |
| 177 | 8400 | 8652 |
| 178 | 7000 | 7210 |
| 179 | 6200 | 6386 |
| 180 | 3200 | 3296 |
| 181 | 3100 | 3193 |
| 182 | 2500 | 2575 |
| 183 | 2800 | 2884 |
| 184 | 4200 | 4326 |
| 185 | 4100 | 4223 |
| 186 | 3400 | 3502 |
| 187 | 3000 | 3090 |
| 188 | 3800 | 3914 |
| 189 | 3600 | 3708 |
| 190 | 2900 | 2987 |
| 191 | 2500 | 2575 |
| 192 | 4000 | 4120 |
| 193 | 3900 | 4017 |
| 194 | 3200 | 3296 |
| 195 | 2800 | 2884 |
| 196 | 3100 | 3193 |
| 197 | 3000 | 3090 |
| 198 | 2600 | 2678 |
| 199 | 2600 | 2678 |
| 200 | 4400 | 4532 |
| 201 | 13000 | 13390 |
| 202 | 6000 | 6180 |
| 203 | 6500 | 6695 |
| 204 | 10000 | 10300 |
| 205 | 12008 | 12368.24 |
| 206 | 8300 | 8549 |
Most mathematical operations or equations can be easily incorporated into your coding solutions using traditional operators (e.g., +, -, *, /). When performing division, ensure the output values do not deviate from the expected values. The handling of integer division varies among relational database management systems and “banker’s” rounding may occur.
Attribute Aliases
In the previous two examples, attributes were derived based on a combination of pre-existing attributes, scalar values, and the output of built-in functions. Using the AS keyword, a name for each derived attribute was provided. Use the AS keyword to provide meaningful names to derived attributes. If a name is not provided in the query, the attribute name will reflect its derivation. For example, the new_salary attribute above would read as “salary*1.03” in the absence of the AS keyword and a user-specified name.
Common Errors
One of the most common errors observe new SQL users make is including a trailing comma in the SELECT clause. When a comma follows the final expression specified in the SELECT clause, the relational database management system (RDBMS) will return an error. For Oracle, that error is ORA-00936: missing expression. If you remember the purpose of the commas in the SELECT clause is to separate the expressions listed, then this error message is rather helpful and makes sense. The RDBMS is expecting an expression to follow the comma!
Another error I frequently see new SQL users make is not including the necessary number of commas in the SELECT clause when more than one expression is specified. For example, suppose a query contains four expressions in the SELECT clause and the comma required after expression two is missing. When that query is executed, Oracle database will return the error, ORA-00923: FROM keyword not found where expected.
SQL veterans and new users alike make errors; the difference is experience in troubleshooting. Understanding a handful of common errors will allow you to jump to specific sections of your coding solution to resolve the errors quickly instead of requiring a more lengthy, top-down review.
Knowledge Check
If you’d like to confirm your understanding on the SELECT clause and other fundamental concepts, consider solving a handful of my practice problems here.