Retrieving Data PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

PENGANTAR

BASIS DATA

MUHAMMAD RUDYANTO ARIEF, MT


[email protected]
Retrieving Data Using the SQL SELECT
Statement
Objectives
 After completing this lesson, you should be able to do the
following :
– List the capabilities of SQL SELECT statements
– Execute a basic SELECT statement
– Differentiate between SQL statement and Transact-SQL
SQL SELECT Statement
Capabilities of SQL SELECT Statement:
Projection, choose the columns in a table that are returned by a
query. Choose as few or as many of the columns as needed.
Selection, choose the rows in the table that are returned by a
query. Various criteria can be used to restrict the rows that are
retrieved.
Joining, bring together data that is stored in different tables by
specifying the link between them.
Projection

Table 1
Selection

Table 1
Join

Join

Table 1 Table 2
Basic Syntax
SELECT select_list
[ INTO new_table ]
FROM table_source
[ WHERE search_condition ]
[ GROUP BY group_by_expression ]
[ HAVING search_condition ]
[ ORDER BY order_expression [ ASC | DESC ] ]

 SELECT identifies the columns to be displayed.


 FROM identifies the table containing those columns.
Basic Syntax
SELECT * | { [DISTINCT]
column|expression [alias],...}
FROM table;
Selecting All Columns
SELECT * FROM table_source;

Note : * = All Fields/ Columns

Example :

SELECT * FROM employees;


SELECT * FROM nasabah;
Selecting Spesific Columns
SELECT field1,field2,…,fieldn
FROM table_source;

Example :

SELECT employee_id,first_name,last_name,salary
FROM hr.employees;

SELECT kode_cabang,nama_cabang,alamat_cabang
FROM cabang_bank;
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.
 Clauses are usually placed on separate lines.
 Indents are used to enhance readability.
 In Query Analyzer, SQL statements can optionally be terminated by a
semicolon (;). Semicolons are required if you execute multiple SQL
statements.
Arithmetic Expression
Create expressions with number and date data
by using arithmetic operators.

Operator Description
+ Penjumlahan
- Pengurangan
* Perkalian
/ Pembagian
Using Arithmatic Expression
SELECT first_name,salary,salary+300
FROM employees;

SELECT first_name,salary,salary*0.1
FROM employees;

SELECT employee_id,first_name,salary,salary-100
FROM employees;
Operator Precedence Rules
SELECT
no_rekening, saldo, 12 * saldo + 300000
FROM rekening;

SELECT
no_rekening, saldo, 12*(salary + 300000)
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 a zero or a blank space.
SELECT no_rekening, saldo
FROM rekening;
SELECT no_rekening, saldo, 12 * saldo
FROM rekening;
Null Values in Arithmetic Expressions

 Arithmetic expressions containing a null value evaluate to


null.

SELECT last_name, 12*salary*commission_pct


FROM employees;
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 if it is case-sensitive
Using Column Aliases

SELECT id_nasabah AS nasabah


FROM nasabah;

SELECT id_nasabah AS “Nasabah”,


saldo, saldo * 12 AS “Saldo Per Tahun”
FROM nasabah;
Concatenation Operator
 A concenation operator :
– Links columns or character strings to other columns
– Is represented by a plus sign (+)
– Creates a resultant column that is a character expression

SELECT nama_nasabah +
alamat_nasabah AS “Nasabah”
FROM nasabah;
Duplicate Rows
 The default display of queries is all rows, including duplicate
rows.
SELECT kode_cabang 1

FROM rekening;

SELECT DISTINCT kode_cabang


2
FROM rekening;
summary
 In this lesson, you should have learned how to :
– Write SELECT statement that :
• Returns all rows and columns from a table
• Returns specified columns from a table
• Uses column aliases to display more descriptive column headings
– Use Query Analyzer environment to write, save, and execute SQL
statements and Transact-SQL commands
Selesai… Terima Kasih

You might also like