Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Database Systems Lecture2

This document discusses the SQL SELECT statement and how to retrieve data from database tables. It covers: 1) The basic syntax of the SELECT statement and how to select all columns or specific columns from a table. 2) Additional SELECT statement topics like selecting from multiple tables, filtering rows, sorting results, and using column aliases. 3) Common SQL operators for comparisons, arithmetic, and concatenating strings. 3) Guidelines for properly structuring SQL statements and clauses for readability.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Database Systems Lecture2

This document discusses the SQL SELECT statement and how to retrieve data from database tables. It covers: 1) The basic syntax of the SELECT statement and how to select all columns or specific columns from a table. 2) Additional SELECT statement topics like selecting from multiple tables, filtering rows, sorting results, and using column aliases. 3) Common SQL operators for comparisons, arithmetic, and concatenating strings. 3) Guidelines for properly structuring SQL statements and clauses for readability.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Database Systems

T. Amani Al-Kebsi Lecture 2


Retrieving Data Using
the SQL SELECT Statement

SQL Syntax
▪ SQL is followed by unique set of rules and guidelines called
Syntax.
▪ All the SQL statements start with any of the keywords like SELECT,
INSERT, UPDATE, DELETE, ALTER, DROP, CREATE, USE, SHOW
and all the statements end with a semicolon (;).

▪ Important point to be noted is that SQL is case insensitive, which


means SELECT and select have same meaning in SQL statement.
SQL SELECT Statement
▪ Selecting All Columns

SELECT * FROM table_name;

▪ Example
SELECT *
FROM departments;
SQL SELECT Statement
▪ Selecting Specific Columns

SELECT column1, column2....columnN FROM table_name


▪ Example
SELECT department_id, location_id
FROM departments;
SQL SELECT Statement
▪ Show all the tables of database

SELECT * FROM user_table

▪ Displaying the Table Structure


– Use the DESCRIBE command to display the structure of a table.
– Or, select the table in the Connections tree and use the Columns
tab to view the table structure.

DESC[RIBE] tablename
Writing SQL Statements

➢ SQL statements are not case-sensitive.


➢ SQL statements can be entered on one or more lines.
➢ Keywords cannot be abbreviated or split across lines.
➢ Clauses are usually placed on separate lines.
➢ Indents are used to enhance readability.
➢ In SQL Developer, SQL statements can optionally be terminated by a
semicolon (;). Semicolons are required when you execute multiple SQL
statements.
➢ In SQL*Plus, you are required to end each SQL statement with a semicolon
(;).
SQL Operators

What is an Operator in SQL?

An operator is a reserved word or a character used primarily in an SQL


statement to perform operation(s), such as comparisons and arithmetic
operations.
SQL Operators
SQL Arithmetic Operators:
An operator is a reserved word or a character used primarily in an SQL
statement to perform operation(s), such as comparisons and arithmetic
operations.
SQL Operators

SQL Arithmetic Operators:


SELECT last_name, salary, salary + 300
FROM employees;
SQL Arithmetic Operators:
SELECT last_name, salary, 12*salary+100
FROM employees;

SELECT last_name, salary, 12*(salary+100)


FROM employees;
Defining a Null Value
– Null is a value that is unavailable, unassigned,
unknown, or inapplicable.
– Null is not the same as zero or a blank space.

SELECT last_name, job_id, salary, commission_pct


FROM employees;


Null Values in Arithmetic Expressions

› Arithmetic expressions containing a null value evaluate to null.

SELECT last_name, 12*salary*commission_pct


FROM employees;


Column aliases
Defining a Column Alias
➢ Renames a column heading
➢ Is useful with calculations
➢ Immediately follows the column name (There can also be the optional
AS keyword between the column name and alias.)
➢ Requires double quotation marks if it contains spaces or special
characters, or if it is case-sensitive
Using Column Aliases

SELECT last_name AS name, commission_pct comm


FROM employees;

SELECT last_name "Name" , salary*12 "Annual Salary"


FROM employees;
Concatenation Operator
› A concatenation operator:
– Links columns or character strings to other columns
– Is represented by two vertical bars (||)
– Creates a resultant column that is a character expression

SELECT last_name||job_id AS "Employees"


FROM employees;


Using Literal Character Strings

SELECT last_name ||' is a '||job_id


AS "Employee Details"
FROM employees;


Alternative Quote (q) Operator
– Specify your own quotation mark delimiter.
– Select any delimiter.
– Increase readability and usability.
SELECT department_name || ' Department' ||
q'['s Manager Id: ]'
|| manager_id
AS "Department and Manager"
FROM departments;
Duplicate Rows

– SQL DISTINCT Clause:


Duplicate Rows
› The default display of queries is all rows, including duplicate
rows.
SELECT department_id
FROM employees;


SELECT DISTINCT department_id
FROM employees;


References
➢ SQL TUTORIAL , Simply Easy Learning by tutorialspoint.com
➢ Oracle Slides

You might also like