Basic SQL
Basic SQL
Insert statement: - Insert statement is used to add/insert new records or from another tables
to a particular table.
Delete statement: - Remove/delete row/s from a table.
Update statement: - Modifies data in existing rows
Select statement: - Used to queries from a single or multiple tables.
- Select - lists the column/s that should be returned from the query.
- From - indicate the table/s or view/s from which data will be obtained
- Where - indicate the conditions under which row/s will be included in the result.
- Group by – indicate categorization of results.
- Having – indicate the conditions under which a category (group) will be included.
- Order by – sorts the result according to specified criteria.
Use Aggregate function: - Use aggregate function such as sum, average, product, count,
max, min, and so on.
Use Boolean operators: - Use AND, OR, and NOT operators for customizing conditions in
where clause.
1.3. Basic SELECT (Query) statement
You can write SQL statements on one line or on many lines.
The SQL statements can be run on SQL tables and views, and database physical and
logical files.
Character strings specified in an SQL statement (such as those used with WHERE
or VALUES clauses) are case sensitive; that is, uppercase characters must be
entered in uppercase and lowercase characters must be entered in lowercase.
WHERE ADMRDEPT='a00' (does not return a result)
WHERE ADMRDEPT='A00' (returns a valid department number)
A SELECT statement can include the following:
1. The name of each column you want to include
2. The name of the table or view that contains the data
3. A search condition to uniquely identify the row that contains the information you want
4. The name of each column used to group your data
5. A search condition that uniquely identifies a group that contains the information you
want
6. The order of the results so a specific row among duplicates can be returned.
The general syntax of SELECT statement looks like this:
SELECT column names
FROM table or view name
WHERE search condition GROUP BY column names
HAVING search condition ORDER BY column-name
The SELECT and FROM clauses must be specified. The
other clauses are optional. With the SELECT clause, you
specify the name of each column you want to retrieve.
The FROM clause specifies the table that you want to select data from. You can select
columns from more than one table.
You can specify that only one column be retrieved or as many as 8000 columns. The
value of each column you name is retrieved in the order specified in the SELECT
clause.
If you want to retrieve all columns (in the same order as they appear in the table's
definition), use an asterisk (*) instead of naming the columns.
Simple selects
Aggregation by groups and groups condition
Sub queries/ sub queries in FROM
Union, Intersect, Except
The simple select statement select clause which used to specify the columns you want to
retrieve, from clause which used to specify the tables from which the columns are to be
retrieved, and the where clause which used to limits the rows returned by your query.
Here is the basic syntax: SELECT <column_list> FROM <table_list> <[WHERE <search_criteria>]>
Simple Selects
The simple select statement select clause which used to specify the columns you want to
retrieve, from clause which used to specify the tables from which the columns are to be
retrieved, and the where clause which used to limits the rows returned by your query.
Here is the basic syntax: SELECT <column_list> FROM <table_list> <[WHERE <search_criteria>]>
1) Usually, a subquery should return only one record, but sometimes it can also return
multiple records when used with operators like IN, NOT IN in the where clause. The
query would be like,
2) Lets consider the student_details table which we have used earlier. If you know the
name of the students who are studying science subject, you can get their id's by using this
query below,
but, if you do not know their names, then to get their id's you need to write the query in this
manner,