Basic SQL

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 32

SQL

STRUCTURE QUERY LANGUAGE

Database
basis data (bahasa Inggris: database), atau sering pula dieja basisdata, adalah
kumpulan informasi yang disimpan di dalam komputer secara sistematik sehingga dapat
diperiksa menggunakan suatu program komputer untuk memperoleh informasi dari basis data
tersebut.
Well-known DBMSs include MySQL, PostgreSQL, Microsoft SQL Server, Oracle, SAP and IBM DB2.

Database
Microsoft SQL Server,Oracle,Sybase,Interbase,XBase,Firebird,MySQL,PostgreSQL,Microsoft
Access,dBase III,Paradox,FoxPro,Visual
FoxPro,Arago,Force,Recital,dbFast,dbXL,Quicksilver,Clipper,FlagShip,Harbour,Visual
dBase,Lotus Smart Suite Approach,db2,MongoDB
According to DB-Engines, the most popular systems are Oracle, MySQL, Microsoft SQL
Server, PostgreSQL and IBM DB2.

Database No SQL
Microsoft SQL Server,Oracle,Sybase,Interbase,XBase,Firebird,MySQL,PostgreSQL,Microsoft
Access,dBase III,Paradox,FoxPro,Visual
FoxPro,Arago,Force,Recital,dbFast,dbXL,Quicksilver,Clipper,FlagShip,Harbour,Visual
dBase,Lotus Smart Suite Approach,db2,MongoDB
According to DB-Engines, the most popular systems are Oracle, MySQL, Microsoft SQL
Server, PostgreSQL and IBM DB2.

BIG Data
eBay.com uses two data warehouses at 7.5 petabytes and 40PB as well as a 40PB Hadoop cluster for search,
consumer recommendations, and merchandising. Inside eBays 90PB data warehouse
Amazon.com handles millions of back-end operations every day, as well as queries from more than half a million
third-party sellers. The core technology that keeps Amazon running is Linux-based and as of 2005 they had the
worlds three largest Linux databases, with capacities of 7.8 TB, 18.5 TB, and 24.7 TB.[36]
Walmart handles more than 1 million customer transactions every hour, which are imported into databases
estimated to contain more than 2.5 petabytes (2560 terabytes) of data the equivalent of 167 times the
information contained in all the books in the US Library of Congress.[1]
Facebook handles 50 billion photos from its user base.[37]

FICO Falcon Credit Card Fraud Detection System protects 2.1 billion active accounts world-wide.[38]
The volume of business data worldwide, across all companies, doubles every 1.2 years, according to
estimates.[39][40]
Windermere Real Estate uses anonymous GPS signals from nearly 100 million drivers to help new home buyers
determine their typical drive times to and from work throughout various times of the day.

BIG Data
Inside eBays 90PB data warehouse. Data is stored in three systems, with about 7.5PB in a Teradata enterprise
data warehouse, 40PB on commodity Hadoop clusters and 40PB on Singularity: a custom system for performing
deep-dive analysis on semi-structured and relational data
Amazon.com handles millions of back-end operations every day, as well as queries from more than half a million
third-party sellers. The core technology that keeps Amazon running is Linux-based and as of 2005 they had the
worlds three largest Linux databases, with capacities of 7.8 TB, 18.5 TB, and 24.7 TB.[36]
Walmart handles more than 1 million customer transactions every hour, which are imported into databases
estimated to contain more than 2.5 petabytes (2560 terabytes) of data the equivalent of 167 times the
information contained in all the books in the US Library of Congress.[1]
Facebook ? here
FICO Falcon Credit Card Fraud Detection System protects 2.1 billion active accounts world-wide.[38]
The volume of business data worldwide, across all companies, doubles every 1.2 years, according to
estimates.[39][40]
Windermere Real Estate uses anonymous GPS signals from nearly 100 million drivers to help new home buyers
determine their typical drive times to and from work throughout various times of the day.

SQL
SQL (/s kju l/ "S-Q-L"; atau Structured Query Language) adalah sebuah bahasa yang digunakan
untuk mengakses data dalam basis data relasional (RDBMS).
SQL was initially developed at IBM by Donald D. Chamberlin and Raymond F. Boyce in the early 1970s.
This version, initially called SEQUEL (Structured English Query Language), was designed to manipulate
and retrieve data stored in IBM's original quasi-relational database management system, System R,
which a group at IBM San Jose Research Laboratory had developed during the 1970s.The acronym
SEQUEL was later changed to SQL because "SEQUEL" was a trademark of the UK-based Hawker
Siddeley aircraft company.
In the late 1970s, Relational Software, Inc. (now Oracle Corporation) saw the potential of the
concepts described by Codd, Chamberlin, and Boyce and developed their own SQL-based RDBMS
with aspirations of selling it to the U.S. Navy, Central Intelligence Agency, and other U.S. government
agencies. In June 1979, Relational Software, Inc. introduced the first commercially available
implementation of SQL, Oracle V2 (Version2) for VAX computers.
After testing SQL at customer test sites to determine the usefulness and practicality of the system,
IBM began developing commercial products based on their System R prototype including System/38,
SQL/DS, and DB2, which were commercially available in 1979, 1981, and 1983, respectively.

SQL - Anatomy

SQL - Operator

SQL Conditional Case


CASE WHEN n > 0
THEN 'positive'
WHEN n < 0
THEN 'negative'
ELSE 'zero'
END

SQL Conditional Case - oracle

SELECT DECODE(n,
1, 'one',
2, 'two',
'i cannot count that high')FROM
some_table;

QUERY
DML (Data Manipulation Language)
UPDATE
DELETE
INSERT

DDL (Data Definition Language)

CREATE
ALTER
TRUNCATE
DROP

Data Type
Character / string
Bit

Number
Datetime

Table example

Table example
tabel BUKU
nama buku jumlah harga
SQL
2 100
2G
3 200
3G
4 300
LTE
5 400
CDMA
6 500
WIMAX
7 600

tabel PEMBELI
PEMBELI BUKU PEMBELIAN
ADI
SQL
1
BUDI
CDMA
2
ARIF
GSM
2
RONI
GSM
1
DIAH
3G
1
TONI
4G
1

SELECT
SELECT tabel.namafield1, namafield2 from tabel

SELECT buku.buku, buku.jumlah, buku.harga FROM buku;

Update
Update namatabel SET namafield = value
UPDATE pembeli SET pembeli.BUKU = "4G"

WHERE (((pembeli.PEMBELI)="TONI"));
PEMBELI
ADI
BUDI
ARIF
RONI
DIAH
TONI

BUKU PEMBELIAN
SQL
1
CDMA
2
GSM
2
GSM
1
3G
1
2G
1

tabel PEMBELI
PEMBELI BUKU PEMBELIAN
ADI
SQL
1
BUDI
CDMA
2
ARIF
GSM
2
RONI
GSM
1
DIAH
3G
1
TONI
4G
1

Delete
DELETE * FROM Namatabel

GROUPING
SELECT namafield, ... FROM Namatabel GROUP BY namafield
SELECT pembeli.BUKU, Var(pembeli.PEMBELIAN) AS VarOfPEMBELIAN

FROM pembeli
GROUP BY pembeli.BUKU;

GROUPING - result
Tabel

tabel PEMBELI
PEMBELI BUKU PEMBELIAN
ADI
SQL
1
BUDI
CDMA
2
ARIF
GSM
2
RONI
GSM
1
DIAH
3G
1
TONI
4G
1

Hasil
BUKU SumOfPEMBELIAN
3G
1
4G
1
CDMA
2
GSM
3
SQL
1

JOIN
INNER JOIN
LEFT JOIN

RIGHT JOIN
OUTER JOIN
CROSS JOIN

INNER JOIN
Tersedia di dua tabel
SELECT tabel_kiri.*, tabel_kanan.* FROM tabel_kiri inner join tabel_kanan on
tabel_kiri.field = tabel_kanan.field

Example :
SELECT buku.buku, buku.jumlah, buku.harga, pembeli.PEMBELI,
pembeli.BUKU, pembeli.PEMBELIAN
FROM buku INNER JOIN pembeli ON buku.buku = pembeli.BUKU;

INNER JOIN - result


tabel BUKU
nama buku jumlah harga
SQL
2 100
2G
3 200
3G
4 300
LTE
5 400
CDMA
6 500
WIMAX
7 600

tabel PEMBELI
PEMBELI BUKU PEMBELIAN
ADI
SQL
1
BUDI
CDMA
2
ARIF
GSM
2
RONI
GSM
1
DIAH
3G
1
TONI
4G
1

BUKU.BUKU jumlah harga PEMBELI


SQL
2 100 ADI
3G
4 300 TONI
3G
4 300 DIAH
CDMA
6 500 BUDI

PEMBELI.BUKU PEMBELIAN
SQL
1
3G
1
3G
1
CDMA
2

LEFT JOIN

LEFT JOIN
Semua di tabel kiri ditampilkan
SELECT tabel_kiri.*, tabel_kanan.* FROM tabel_kiri LEFT JOIN tabel_kanan on tabel_kiri.field =
tabel_kanan.field
SELECT buku.buku, buku.jumlah, buku.harga, pembeli.PEMBELI,
pembeli.BUKU, pembeli.PEMBELIAN
FROM buku LEFT JOIN pembeli ON buku.buku = pembeli.BUKU;

LEFT JOIN - result


tabel BUKU
nama buku jumlah harga
SQL
2 100
2G
3 200
3G
4 300
LTE
5 400
CDMA
6 500
WIMAX
7 600

tabel PEMBELI
PEMBELI BUKU PEMBELIAN
ADI
SQL
1
BUDI
CDMA
2
ARIF
GSM
2
RONI
GSM
1
DIAH
3G
1
TONI
4G
1

BUKU.BUKU jumlah harga PEMBELI


SQL
2 100 ADI
2G
3 200
3G
4 300 TONI
3G
4 300 DIAH
LTE
5 400
CDMA
6 500 BUDI
WIMAX
7 600

PEMBELI.BUKU PEMBELIAN
SQL
1
3G
3G

1
1

CDMA

RIGHT JOIN

RIGHT JOIN
Semua di tabel kanan ditampilkan
SELECT tabel_kiri.*, tabel_kanan.* FROM tabel_kiri RIGHT JOIN tabel_kanan on tabel_kiri.field =
tabel_kanan.field
SELECT buku.buku, buku.jumlah, buku.harga, pembeli.PEMBELI,
pembeli.BUKU, pembeli.PEMBELIAN
FROM buku RIGHT JOIN pembeli ON buku.buku = pembeli.BUKU;

RIGHT JOIN - result


tabel BUKU
nama buku jumlah harga
SQL
2 100
2G
3 200
3G
4 300
LTE
5 400
CDMA
6 500
WIMAX
7 600

tabel PEMBELI
PEMBELI BUKU PEMBELIAN
ADI
SQL
1
BUDI
CDMA
2
ARIF
GSM
2
RONI
GSM
1
DIAH
3G
1
TONI
4G
1

BUKU.BUKU jumlah harga PEMBELI


SQL
2 100 ADI
CDMA
6 500 BUDI
ARIF
RONI
3G
4 300 DIAH
3G
4 300 TONI

PEMBELI.BUKU PEMBELIAN
SQL
1
CDMA
2
GSM
2
GSM
1
3G
1
3G
1

OUTER JOIN

CROSS JOIN

ALIAS
Select field as alias from tabel
Example :
SELECT buku AS nama_buku, jumlah, harga FROM buku;
tabel BUKU
nama buku jumlah harga
SQL
2 100
2G
3 200
3G
4 300
LTE
5 400
CDMA
6 500
WIMAX
7 600

nama_buku jumlah harga


SQL
2 100
2G
3 200
3G
4 300
LTE
5 400
CDMA
6 500
WIMAX
7 600

You might also like