You are on page 1of 5

Komanda

AND / OR

ALTER
TABLE

AS (alias)

BETWEEN

Primjer
SELECT column_name(s)
FROM table_name
WHERE condition
AND|OR condition
ALTER TABLE table_name
ADD column_name datatype
or
ALTER TABLE table_name
DROP COLUMN column_name
SELECT column_name AS column_alias
FROM table_name
or
SELECT column_name
FROM table_name AS table_alias
SELECT column_name(s)
FROM table_name
WHERE column_name
BETWEEN value1 AND value2

CREATE
DATABASE

CREATE DATABASE database_name

CREATE
TABLE

CREATE TABLE table_name


(
column_name1 data_type,
column_name2 data_type,
column_name3 data_type,
...
)

CREATE
INDEX

CREATE
VIEW

DELETE

DROP
DATABASE
DROP
INDEX

CREATE INDEX index_name


ON table_name (column_name)
or
CREATE UNIQUE INDEX index_name
ON table_name (column_name)
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
DELETE FROM table_name
WHERE some_column=some_value
or
DELETE FROM table_name
(Note: Deletes the entire table!!)
DELETE * FROM table_name
(Note: Deletes the entire table!!)
DROP DATABASE database_name
DROP INDEX table_name.index_name
(SQL Server)
DROP INDEX index_name ON table_name
(MS Access)
DROP INDEX index_name (DB2/Oracle)
ALTER TABLE table_name

Implementacija
SELECT * FROM Customers
WHERE Country='Germany'
AND City='Berlin';
ALTER TABLE Persons
ADD DateOfBirth date

SELECT CustomerName,
Address+', '+City+',
'+PostalCode+', '+Country
AS Address
FROM Customers;
SELECT * FROM Products
WHERE ProductName
BETWEEN 'C' AND 'M';

CREATE TABLE Persons


(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);

DELETE FROM Customers


WHERE
CustomerName='Alfreds
Futterkiste' AND
ContactName='Maria
Anders';

DROP INDEX index_name (MySQL)

DROP
TABLE

EXISTS

GROUP BY

HAVING

IN

INSERT
INTO

INNER JOIN

LEFT JOIN

DROP TABLE table_name


IF EXISTS (SELECT * FROM table_name
WHERE id = ?)
BEGIN
--do what needs to be done if exists
END
ELSE
BEGIN
--do what needs to be done if not
END
SELECT column_name,
aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
SELECT column_name,
aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING
aggregate_function(column_name)
operator value
SELECT column_name(s)
FROM table_name
WHERE column_name
IN (value1,value2,..)
INSERT INTO table_name
VALUES (value1, value2, value3,....)
or
INSERT INTO table_name
(column1, column2, column3,...)
VALUES (value1, value2, value3,....)

SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON
table_name1.column_name=table_name2
.column_name

SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON
table_name1.column_name=table_name2
.column_name

SELECT * FROM Customers


WHERE City IN
('Paris','London');
# ILI #

SELECT
Customers.CustomerName,
Orders.OrderID
FROM Customers
INNER JOIN Orders
ON
Customers.CustomerID=Ord
ers.CustomerID
ORDER BY
Customers.CustomerName;
SELECT
Customers.CustomerName,
Orders.OrderID
FROM Customers
LEFT JOIN Orders
ON
Customers.CustomerID=Ord
ers.CustomerID

ORDER BY
Customers.CustomerName;

RIGHT JOIN

FULL JOIN

SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON
table_name1.column_name=table_name2
.column_name

SELECT column_name(s)
FROM table_name1
FULL JOIN table_name2
ON
table_name1.column_name=table_name2
.column_name

SELECT
Customers.CustomerName,
Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders
ON
Customers.CustomerID=Ord
ers.CustomerID
ORDER BY
Customers.CustomerName;
SELECT * FROM Customers
WHERE City LIKE 's%';
# poinje sa s #
SELECT * FROM Customers
WHERE City LIKE '%s';
# zavrava sa s #

LIKE

SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern

SELECT * FROM Customers


WHERE Country LIKE '%land
%';
# sadri dio land #
SELECT * FROM Customers
WHERE Country LIKE '____';
# 4 bilo koja karaktera #
SELECT * FROM Customers
WHERE Country LIKE '[ac]at';
# moe aat bat i cat #

ORDER BY
SELECT
SELECT *
SELECT
DISTINCT
SELECT
INTO

SELECT column_name(s)
FROM table_name
ORDER BY column_name [ASC|DESC]
SELECT column_name(s)
FROM table_name
SELECT *
FROM table_name
SELECT DISTINCT column_name(s)
FROM table_nameSELECT DISTINCT City
FROM Customers;
SELECT *
INTO new_table_name [IN

SELECT * FROM Customers


ORDER BY Country DESC;
SELECT CustomerName,City
FROM Customers;

externaldatabase]
FROM old_table_name
or
SELECT column_name(s)
INTO new_table_name [IN
externaldatabase]
FROM old_table_name

SELECT TOP

SELECT TOP number|percent


column_name(s)
FROM table_name

TRUNCATE
TABLE

TRUNCATE TABLE table_name

UNION

SELECT column_name(s) FROM


table_name1
UNION
SELECT column_name(s) FROM
table_name2

UNION ALL

SELECT column_name(s) FROM


table_name1
UNION ALL
SELECT column_name(s) FROM
table_name2

UPDATE

UPDATE table_name
SET column1=value, column2=value,...
WHERE some_column=some_value

WHERE

SELECT column_name(s)
FROM table_name
WHERE column_name operator value

SELECT *
INTO
CustomersBackup2013
FROM Customers
WHERE Country='Germany';

SELECT TOP 2 * FROM


Customers;
# dva najvea #
SELECT TOP 50 PERCENT *
FROM Customers;
# pola najveih #

SELECT City FROM


Customers
UNION
SELECT City FROM Suppliers
ORDER BY City;
SELECT City, Country FROM
Customers
WHERE Country='Germany'
UNION ALL
SELECT City, Country FROM
Suppliers
WHERE Country='Germany'
ORDER BY City;
UPDATE Customers
SET ContactName='Alfred
Schmidt', City='Hamburg'
WHERE
CustomerName='Alfreds
Futterkiste';
SELECT * FROM Customers
WHERE CustomerID=1;

Online editor:
http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all [Log]
Primjeri:
select price, count(*) from fake_apps group by price Sortira aplikacije po
cijenama i stavlja broj aplikacija
select price, count(*) from fake_apps where downloads>20000 group by price
Slino, samo aplikacije sa preko 20000 downloada
select sum(downloads) from fake_apps Suma svih downloada

select max(downloads) from fake_apps Najvei broj downloada


select name, category, min(downloads) from fake_apps group by category
Izdvaja aplikaciju sa najmanje downloada u kategoriji
select avg(downloads) from fake_apps Prosjean broj downloada
select price, avg(downloads) from fake_apps group by price Prosjean broj
downloada po cijeni
select price, round(avg(downloads),2) from fake_apps group by price
Zaokruivanje na dvije decimale
Ubacivanje podataka u tabelu:
http://dev.mysql.com/doc/refman/5.7/en/loading-tables.html (koristi se TAB da
razmakne podatke!)

SELECT ProductID, ProductName, Price from Products where not (Price between
19 and 100);