Basic SQL Training .v2
Basic SQL Training .v2
The SELECT function is a safe function that will not allow you to screw up the
database (or DB)
This program is often just under your programs, but may be hidden, as is above, under
another directory. Log in, selecting the server required.
If a blank screen comes up, press F8, which will bring up the below screen.
The top left hand highlighted shows the server the database is situated on. The top
highlighted box shows the database that is being looked at, and the bottom box shows
the expandable
Using v723, expand the left hand pane option- ( v723- User tables)
To execute the
statement, hit this button
The user can now see what tables are available, and by expanding the next level down
will show the columns that are in the table.
The basic form of a select function are:
SELECT
FROM
WHERE
The use of the star or *, will bring everything back in that table.
eg, select * from cs_broker, however, structure the query as above, putting the new
query in the next line.
Select *
From cs_broker
The results from this query are then given in the bottom right box, with all columns
and all rows. Also note that there are a total 30 rows.
Command box
Results window
By expanding the tables on the left box, the user can ascertain the columns in each
table.
If the user only required several of the columns that were in the table rather that all of
them, this can be done by specifying each column required. For instance, the user
only requires, order_id, status, security_id , all from the ts_order table.
The statement to retrieve only these would be:
Select order_id, sec_id, status
From ts_order
To take the statement further, the user may only want to see the above statement with
securities that have a READY status.
Select order_id, sec_id, status
From ts_order
Where status = READY
The use of the where statement is now used to define the columns status. The status
must be within single quotes.
NB- the information within the quotes is note case sensitive, however, for oracle it is.
Joining
Joining tables is required when some columns do not show all of the data is required
for the query that is required.
Perform the following statement:
Select order_id, trans_type, sec_id, status
From ts_order
Where status = READY
Notice that when the query has been run, and that there are two queries in the top box,
there two box appear below. This is sometime useful, however, to receive only one
box, highlight the required statement, and then run.
If the user requires data from two different tables, aliases are required to associate the
two tables. This basically means, using the common data between the two tables, a
relational query is then created.
Select *
From csm_security
Sec_id is a common denominator for these two tables, therefore, it is possible to link
these two tables, using this common factor.
Using the above information, it is then possible to associate the csm_security table to
the ts_order table, by using association.
Associate sec_id(in the ts_order table) to sec_name ( in the csm_security table)
By adding the csm_security to the from statement will create an alias.
This statement has now brought back all the securities that are in the csm_security
table, and ts_order table that have a status of ready, returning their name, and the
trans_type, and order_id, using the order_id as the common denominator.
Try this:
Run a query to find all orders in the ready status
Pick one of the orders- find the allocations for that one order.
Now show the account name for each of those allocations
Basic Examples
select*
fromts_order
wheremanager='XBBJNW9'
and
trans_type='SELLL'
and
order_acct_cd='GEMZ'
and
trade_datebetweento_date('01/01/2009','dd/mm/yyyy')andto_date
('29/12/2009','dd/mm/yyyy')
select*
fromcsm_security
wheresec_typ_cd='CURR'