You are on page 1of 11

Introduction …

What is SQL ?

• Pronounced as “SeQuel”
• Refers to Structured Query Language.
• SQL is the language used to query a database in order to retrieve data from, add
data to, or, modify the data in, the database and to structure/restructure the database
itself.

Database

• A database is anything that is expected to hold data. It can be anything from your
notebook to digitally synchronized software.
• Most commonly used digital databases include MS Excel, MS Access, My SQL,
Oracle, Microsoft SQL Server.
• These databases use tables to store the data in them and are designed around the
concepts of Relational Database Management System (RDBMS).
What is AQT ?

• AQT is known as Advance Query Tool.


• AQT is an application placed between you and your core database to simplify things
to an extent by providing you a user friendly interface to write and execute queries,
manage multiple databases and provide various other options such as exporting
result to file etc.
• AQT is not a database, it is just an application.

Facts

• Oracle is the database that PeopleSoft Uses.


• While functioning, the queries that are executed in AQT are checked and then sent to
the core database for fetching data. This means running queries at AQT is slower
than running queries in core database.
SQL Statements ...
The Select Statement

“SQL is the language used to query a


database in order to retrieve data from,
add data to, or, modify the data in, the
database and to structure/restructure the
database.”
• Syntax – Select [Field] from [Table] where [Condition];

• Select – Tells the database that we need to retrieve data.


From – Where is the data that we want to retrieve.
Where – Data matching some criteria, if any.

• Field are the columns from a specified table. This a mandatory part because we need to search
something.

• Table is the warehouse where the related data is stored in defined fields. This a mandatory part
because this part tells the database that where the requested fields are located.

• Condition part is NOT mandatory while defining a Select Statement as you may need all the data
from a selected table.

• Having said Condition part is optional, keyword “Where” becomes optional, because “Where” is
only used to define conditions.

Select emplid from ps_job;

Select emplid, deptid, jobcode from ps_job where emplid = ‘xxxxxxxxx’;

Select * from ps_job where emplid = ‘xxxxxxxxx’;


Select emplid from ps_job;

• This query is our first example from previous slide.


• As we can see it has some usual parts …
Keywords – select and from
Field – emplid
Table – ps_job
• Why is there a semi colon (;) after our usual select statement?
This is because in computer terminology a semi colon indicates an end of
statement. So basically this semi colon will tell the system where does your
one query ends and other starts.
• Any query written in SQL needs to be ended with a semi colon irrespective of
its type or keyword used.
• This query will fetch you all the employee ids from ps_job table. Interestingly
at this point our database will simply send all the rows in the ps_job table
and will not really care to send the distinct records.
• How to get distinct records ? Will let you know later. For now, it is possible.
Select emplid, deptid, jobcode from ps_job where emplid = ‘xxxxxxxxx’;

In this example we have a few more additions…

• Multiple Fields are separated by comma (,) – emplid, deptid, jobcode


A comma is always placed between two fields that need to be fetched i.e. there will always be (n-1)
commas for any (n) fields fetched.
Commas are always placed between the fields i.e. it will never be placed before first field or after
the last field that needs to be fetched.
• Next is our favorite where clause. This lets us define a criteria by which we want to fetch data –
where emplid = ‘xxxxxxxxx’
Where clause is used to define the criteria, here, our criteria is emplid = ‘xxxxxxxxx’ i.e. fetch
emplid, deptid, jobcode from ps_job table where employee id is equal to any 9 digit id.
• Lastly, when we try to give database a value for a field not defined as numeric, we need to enclose
it in the inverted commas. However fields defined as numeric will not be enclosed in inverted
commas.

For Example:

Select emplid, deptid, jobcode from ps_job where emplid = ‘012345678’;


Select emplid, deptid, jobcode from ps_job where empl_rcd = 0;

• Data type of a field is defined when it is created in a table. The logic is same as it is in MS Access.
Select * from ps_job where emplid = ‘xxxxxxxxx’;

• In this example we have introduced a new operator – Asterisk (*)


• In case we need to fetch all the fields from a table the this operator is used.

Points to Remember

• A “Select Statement” has 3 parts – Fields, Table and Criteria.


• Fields and Table are mandatory parts while Criteria is optional.
• Every query written in SQL must be ended by a semi colon (;).
• Multiple fields can be fetched by using commas between 2 or more fields that need to
be fetched.
• Data defined in the criteria part for a non numeric field is enclosed in inverted
commas.
• Data defined in the criteria part for a numeric field is not enclosed in inverted commas.
• Asterisk Operator is used to fetch all the fields from a table.
Multiple Conditions (Criteria) And
Related Keywords & Operators
• It may be a case that the query you want to pull from the database may consist of data based on
one or multiple conditions. In such cases, we need to use some keywords and operators as per
our requirements.

• Basic Keywords:
And, Or, In, Like

• Keywords ‘And’ & ‘Or’ are used to define the multiple conditions.

• ‘In’ is used to search data defined within a set of values.


• Basic Operators:

= (equal) , > (less than), < (greater than), <> (not equal to), >= (less than or equal to), <= (greater
than or equal to)
• The above 5 operators will help you to create conditions based on exact values. For example:
emplid = ‘xxxxxxxxx’ / amount >= 500.00 / name <> ‘Jatin Sethi’ etc.
• ‘Like’ is used when we need to search data similar to what we have defined, but, not exactly
what we are defining. For Example: Search all the names starting from Alphabet ‘A’.
Operator: AND
• Fetches a row from the table if all the conditions mentioned stand true.

Example: Select * from ps_job where deptid = ‘12345’ and jobcode = ‘432456’;

• This query will fetch all the records from the Job Data table where dept. id = ‘12345’ and Job
Code = ‘432456’

Operator: OR
• Fetches a row from the table if either of the conditions mentioned stand true.

Example: Select * from ps_job where deptid = ‘12345’ or jobcode = ‘432456’;


• This query will fetch all the records from the Job Data table where dept. id = ‘12345’ or job code =
‘432456’ .

Operator: IN
• In operator is used when data needs to be fetched from a set of values.

Example: Select * from ps_job where deptid in (‘12345’, ‘13458’, ‘13698’);


• This query will fetch all the records from the Job Data table where dept. id = ‘12345’ or ‘13458’ or
‘13689’.

You might also like