0% found this document useful (0 votes)
17 views

DB Lab 1 - Basic SQL Statements

The document discusses the basics of SQL and relational databases. It covers SQL statements like SELECT, data types, NULL values, joins, and other core concepts. Examples are provided to illustrate the content.

Uploaded by

Reem Tarek
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

DB Lab 1 - Basic SQL Statements

The document discusses the basics of SQL and relational databases. It covers SQL statements like SELECT, data types, NULL values, joins, and other core concepts. Examples are provided to illustrate the content.

Uploaded by

Reem Tarek
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Database Lab 1:

Basic SQL Statements


CC414
Objectives

Understanding basics of a Relational Model


Understanding basics of SQL statements
Capabilities of SQL SELECT statements
Execution of SELECT statements
The Relational Model
Basically a “spreadsheet with rules”

Table Columns/Fields/Attributes

Rows/Records/Tuples
Characteristics of a Relational Model
Tables are flat: eliminates potential for redundant data & requires primary key
Attributes have types & are static, eg: INT, FLOAT, VARCHAR(20),
DATETIME
Order of the records does not matter, but duplicate records violate rules
(semantics)
Characteristics of a Relational Model
Assuming the attribute “Salary” is of Sub-tables are not allowed, additional
type INT, the first record in this table data can be stored in other tables with
violates the attribute type. relations
What is SQL?
Structured Query Language
Declarative query language, which is a critical tool for data analysis
Other languages like Python/Java are Procedural languages
With declarative languages, you tell the computer what you want, not how to get
it
Declarative query languages allow physical data independence, meaning
physical storage level is changed without affecting conceptual view of data
SQL “SELECT” Statement
SELECT [DISTINCT]{*, column [[as] alias],...}
FROM table;

In basic form: In this syntax:


◦ SELECT: identifies which ◦ DISTINCT: removes duplicates
column ◦ * : selects all columns
◦ FROM: identifies which table ◦ Column: selects names column
containing columns listed in ◦ Alias: gives selected columns
SELECT clause different naming
◦ Table: name of table specified
Note: Course Terminology
Keyword: refers to an individual SQL element
◦ For eg: SELECT and FROM are keywords

Clause: is part of an SQL statement


◦ For eg: SELECT id,name … is a clause

Statement: combination of two or more clauses


◦ For eg: SELECT *FROM employee is a SQL statement
Writing SQL Statements
◦ SQL statements are not case sensitive
◦ SQL statements can be on one or more lines
◦ Keywords cannot be abbreviated or split across lines
◦ Tabs and indents are used to enhance readability
◦ Keywords typically are entered in uppercase; all other words, such as table
names and columns, are entered in lowercase
◦ Clauses are usually placed on separate lines for readability and ease of editing
Capabilities of SELECT Statements
Selection: to choose the rows in a
table that you want returned by a
query.

Projection: to choose the columns in a


Selection Projection table that you want returned by your
query

Join Join: to bring together data that is


stored in different tables by creating
a link through a column that both the
tables share

Table 1 Table 2
Selecting All Columns
SQL> SELECT *
2 FROM dept;

DEPTNO DNAME LOC


--------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
Selecting Specific Columns
SQL> SELECT deptno, loc
2 FROM dept;

DEPTNO LOC
--------- -------------
10 NEW YORK
20 DALLAS
30 CHICAGO
40 BOSTON
Column Heading Deafults
Default justification
◦ Left: Date and character data
◦ Right: Numeric data

Default display: Uppercase


Arithmetic Expression
Create expressions on NUMBER and DATE data by using arithmetic operators
SQL> SELECT ename, sal, sal+300
2 FROM emp;

ENAME SAL SAL+300


---------- --------- ---------
KING 5000 5300
BLAKE 2850 3150
CLARK 2450 2750
JONES 2975 3275
MARTIN 1250 1550
ALLEN 1600 1900
...
14 rows selected.
Operator Precedence
SQL> SELECT ename, sal, 12*sal+100
2 FROM emp; Multiplication and Division
take priority over
addition/subtraction
ENAME SAL 12*SAL+100
---------- --------- ---------- Operators of same priority
KING 5000 60100
BLAKE 2850 34300 are evaluated from left to
CLARK 2450 29500 right
JONES 2975 35800
MARTIN 1250 15100 Parentheses are used to set
ALLEN 1600 19300 priority and clarify
...
14 rows selected. statements
Using Parentheses
SQL> SELECT ename, sal, 12*(sal+100)
2 FROM emp;

ENAME SAL 12*(SAL+100)


---------- --------- -----------
KING 5000 61200
BLAKE 2850 35400
CLARK 2450 30600
JONES 2975 36900
MARTIN 1250 16200
...
14 rows selected.
Defining a NULL value
A null is a value that is unavailable,
SQL> SELECT ename, job, comm unassigned, unknown, or inapplicable.
2 FROM emp;
A null is not the same as zero or a blank
ENAME JOB COMM space.
---------- --------- ---------
KING PRESIDENT Columns of any datatype can contain null
BLAKE MANAGER values, unless the column was defined as
... NOT NULL or as PRIMARY KEY when
TURNER SALESMAN 0 the column was created.
...
14 rows selected. In the COMM column in the table, you
notice that only a SALESMAN can earn
commission. Other employees are not
entitled to earn commission. A null value
represents that fact.
NULL values in Arithmetic
Expressions
If any column value in an
SQL> select ename NAME, 12*sal+comm arithmetic expression is null,
2 from emp the result is null.
3 WHERE ename='KING';
In the example above,
employee KING is not in
SALESMAN and does not get
NAME 12*SAL+COMM any commission.
---------- -----------
Because the COMM column in
KING
the arithmetic expression is
null, the result is null.
Defining a column alias
SQL> SELECT ename AS name, sal salary
2 FROM emp; Renames a column heading

NAME SALARY Is useful with calculations


------------- ---------
... Immediately follows column
name; optional AS keyword
SQL> SELECT ename "Name",
between column name and alias
2 sal*12 “Annual Salary”
3 FROM emp; Requires double quotation
marks if it contains spaces or
Name Annual Salary special characters or is case
sensitive
------------- -------------
...
Concatenation Operator
SQL> SELECT concat(ename,job) AS "Employees" Concatenates columns or
2 FROM emp; character strings to other
columns
Employees
------------------- Is represented by Concat
KINGPRESIDENT function
BLAKEMANAGER
CLARKMANAGER
JONESMANAGER Creates a resultant column
MARTINSALESMAN that is a character expression
ALLENSALESMAN
...
14 rows selected.
Literal Character Strings
SQL> SELECT concat(ename,' ‘,'is a’,' ‘,job) A literal is a character,
2 AS "Employee Details" expression, or number included
3 FROM emp;
in the SELECT list.

Employee Details Date and character literal values


------------------------- must be enclosed within single
KING is a PRESIDENT
quotation marks.
BLAKE is a MANAGER
CLARK is a MANAGER
JONES is a MANAGER Each character string is output
MARTIN is a SALESMAN once for each row returned.
...
14 rows selected.
Duplicate Row Elimination
SQL> SELECT deptno SQL> SELECT DISTINCT deptno
2 FROM emp; 2 FROM emp;

DEPTNO DEPTNO
--------- ---------
10 10
30 20
10 30
20
...
14 rows selected.
The default display of queries is all rows, including duplicate
rows
Eliminate duplicate rows by using the DISTINCT keyword in
the SELECT clause
Displaying Table Structure
SQL> DESC dept

Name Null? Type


----------------- -------- ------------
DEPTNO NOT NULL NUMBER(2)
DNAME VARCHAR2(14)
LOC VARCHAR2(13)
Thank You

You might also like