Chapter 2: Query
Select statements could enable users to retrieve data from tables. users can view all fields and records in a table or specify displaying only certain fields and records. in essence, the SELECT statement asks the database a question, which is why it is also known as QUERY.
SELECT [DISTINCT | UNIQUE] (*, columnname [AS] alias, ....)
FROM tablename
[WHERE conditions]
[GROUP BY group_by_expression]
[HAVING group_by_condition]
[ORDER BY (columnname, .....)];Each section begining with a keyword is referred to as a Clause (SELECT CLAUSE, FROM CLAUSE... ).
The only clauses required for the
Select StatementareSELECTandFROM.Square brackets indicate optional portions of the statement.
An
Select Statementends with a semicolon.
Basic Sql Select Statement
-- Selecting All Data From Tables
SELECT * FROM books;
-- Selecting One Column From Tables
SELECT isbn FROM books;
-- Selecting Multiply Columns From Tables
SELECT isbn, title FROM books;
-- Using Column Aliases
-- if the column alias contains spaces or special symbols, or if you do not want it to appear in all uppercase letter, you
-- must enclose it in quotation marks ("")
SELECT isbn, title AS "BOOK's NAME" FROM books;
SELECT isbn, title "BOOK's NAME" FROM books;
SELECT isbn, title books_name FROM books;
-- Using Arithmetic Operations
SELECT isbn, title, retail - cost AS profit FROM books;
-- Null
-- if no value is entered for a column in a row of data, the value is considered `NULL`, indicating an absence of data.
-- `NUll` can lead to undesirable results in operations. If any value in arithmetic operation is `NULL`, the result is `NULL`
SELECT isbn, title, retail - cost - discount AS profit FROM books;
-- Distinct or Unique
-- The Distinct is applied to all column listed in the SELECT Clause to eliminate duplicated rows.
SELECT DISTINCT city, state FROM customers;
SELECT UNIQUE city, state FROM customers;
-- Concatenation
-- Combining the content of two or more columns is known as Concatenation. Using two vertical bars ||
'' single quotation marks here is called String Literal
SELECT firstname || ' ' || lastname FROM customers;
-- Show Structure of Table
-- DESC command is a SQL*PLUS command, not a SQL command. Therefore, a semicolon is not necessary to end with this statement.
DESC customers
DESCRIBE customers
-- List All of Table Information In The Current Account
SELECT * FROM user_tables WHERE table_name = 'customers';The alignment of data value in columns of Select Statement result:
By default, the data for text, or character, fields is left-aligned.
Data for numeric fields is right-aligned.
Oracle 12c does not display insignificant zeros.
Search Conditions
A condition identifies what must exist or a requirement that must be met for a record to be included in the results.
Rules for Character Strings
If there are conditions that compare with character strings, please use the
String Literalthat the character values are enclosed in single quotation marks.Using single quotation marks if the column is defined with anything other than a numeric data type.
You can use a numeric value to compare with a column that is defined as the string type but does not contain any letters. However, it is better to use the corresponding data type.
Issue the
DESCorDESCRIBEcommand to check the data types of columns if you do not make certain the structure of the table.
Rules For Date
Oracle 12c display a data in the default format DD-MON-YY that contains letters and hyphens, MON is the standard three-letters abbreviation for the month. Please make sure the date value is enclosed in single quotation marks.
Comparison Operator
The Order of All Operators:
Arithmetic operators are solved at first.
All of comparison operators are solved at second.
Logical Operators have the low precedence and are evaluated last - in the order
NOT,AND,OR.
Mathematical Comparison Operator*
Operator
Description
=
Equality or "equal to"
>
Greater Than
<
Less Than
<> or != or ^=
Not Equal to
>=
Greater Than or Equal to
<=
Less Than or Equal to
Other Comparison Operator
Operator
Description
[NOT] BETWEEN x AND y
It is used when searching a field for values falling within a specified range.
[NOT] IN (x,y,...)
Similar to a group of OR logical operators. Return records matching anyone of the values listed in the condition.
[NOT] LIKE
It is used with wildcard character to search for patterns.
[NOT] IS NULL
Search the records that does not have entity in one column.
Logical Operator
Operator
Description
AND
Combining two conditions, it is true if both conditions are ture. Otherwise, it is false.
OR
Combining two conditions, it is true if at lease one of both conditions is ture. Otherwise, it is false.
NOT
Reverse the meaning of search conditions rather than combining conditions.
Order By Clause
When sorting in ascending order, values are listed in this order:
Blank and special characters
Numeric values
Character values (Uppercase first)
NULL values.
Ascending order is default, unless you choose the descending order.
Joining Table
Cartesian Join
Cartesian Join, also called Cartesian Product or Cross Join. Means creating a join between tables by displaying any possible records combination. It is very useful when you are performing certain statistical processdures for data analysis.
The Resulting number of rows = {rows in Table 1} {rows in Table 2} {rows in Table N}.
Can be created by two ways: Traditional Method , JOIN Method (CROSS JOIN)
Equality Joins
Equality JOINS is also called Equijoins, inner joins, or simple joins, which is based on two or more tables having equivalent datas stored in a Common Column.
Common Column is a column with the equivalent data existing in two or more tables.
Column Qualifier is used to qualify the common column in SELECT Clause. it can be Table Name or Table Alias.
Advantages of Table Alias:
improve processing efficiency, as system no longer need to identify which table the specific column is in.
coding is simplified when the full table name doesn’t have to be indicated
Rule of Table Alias
If a Table Alias is assigned in the FROM Clause, it must be used any time the table is referenced in that SQL Statement.
Non-Equality JOIN
Joins tables when there are no equivalent rows in the tables to be joined. Often used to match values in one column with a range of values in another column. Can use any comparison operator except the equal sign.
Self-Join
used when a table must be joined to itself to retrieve the data you need. Table aliases are required in the FROM Clause to perform a SELF-JOIN.
Outer-Join
Outer-Join is created when records need to be included in the results without having corresponding records in the join tables. These records are matched with NULL records so that they’re included in the output.
Traditional Method
Deficient Table is the table with missing data.
Outer Join Operator : +, It is placed immediately after the reference to the Deficient Table in the WHERE Clause.
Limitation of Outer Join Operator
The
Outer Join Operatorcan be used for one table in the join condition. In the other words, you cannot createNULLrow in both tables at the same time.A condition that includes the
Outer Join Operatorcan’t use theINorORoperator.
Join Method
Left Outer Join: includes all records from the table listed on the left side of the join, even if no match is found with the other table in the join operation.
Right Outer Join: includes all records from the table listed on the right side of the join, even if no match is found with the other table in the join operation.
Full Outer Join: includes all records from both tables, even if no corresponding record in the other table is found.
Set Operations
Set Operations are used to comibine the results of two (or more) SELECT Statement.
Set Operator
Description
UNION
Return the results of both queries and remove the duplicates.
UNION ALl
Return the results of both queries but includes duplicates.
MINUS
Subtract the results from the second query if they're also returned in the first query's result.
INTERSECT
Return the results includes in results of both queries.
Guidelines For Set Operations
All columns are included to perform the
Set OperationsEach query must have the same number of columns, which are compared positionally.
Column names can be different in each query.
Last updated
Was this helpful?