Data Manipulation Language Slides by Hassaan With Importent Notes
Data Manipulation Language Slides by Hassaan With Importent Notes
Student
Student
INSERT INTO Student ID Name Year
ID Name Year
(Name, ID) 1 John 1
1 John 1 VALUES (‘Mary’, 2) 2 Mary
Student
run
SELECT * FROM Student
WHERE Year = 3
SQL SELECT Overview
SELECT
<column-list>
FROM <table-names>
[WHERE <condition>]
[ORDER BY <column-list>]
[GROUP BY <column-list>]
[HAVING <condition>]
• ([]- optional, | - or)
Simple SELECT
•SELECT
Given a <columns>
table Student with columns
– stuID<table>
FROM
– stuName
– stuAddress
<columns> can be
– stuYear
– A single column
– A comma-separated list of columns
– * for ‘all columns’
Selecting all columns
SELECT *
FROM departments;
Selecting specific columns
Operator Description
+ Add
- Subtract
* Multiply
/ Divide
Using arithmetic operators
…
Operators precedence
__
* // ++
…
Using parenthesis
SELECT last_name, salary, 12*(salary+100)
FROM employees;
…
Defining a NULL value
• A null is a value that is unavailable,
unassigned, unknown, or inapplicable.
• A null is not the same as zero or a blank
space.last_name, job_id, salary, commission_pct
SELECT
FROM employees;
…
NULL values in Arithmetic Expressions
…
Defining a column alias
• 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 is case sensitive
Using column aliases
SELECT last_name AS name, commission_pct comm
FROM employees;
…
Concatenation operator
• A concatenation operator:
– Concatenates columns or character strings to
other columns
– Is represented by two vertical bars (||)
– Creates a resultant column that is a character
expression
Using concatenation operator
…
Literal character strings
…
Duplicate rows
• The default display of queries is all rows,
including duplicate rows.
SELECT
SELECT department_id
department_id
FROM
FROM employees;
employees;
…
Eliminating duplicate rows
• Eliminate duplicate rows by using the
DISTINCT keyword in the SELECT clause.
SELECT DISTINCT department_id
FROM employees;