You are on page 1of 9

SQL

Ստանդարտ լեզու է, օգտագործվում է տվյալների բազային հարցումներ


ուղարկելու համար՝ add, delete, change..., տվյալների բազայում տվյալները
պահելու, մանիպուլյացիաներ անելու, ստանալու համար.
SQL-ի միջոցով միայն հարցումներ ենք ուղարկում տվյալների բազային և այդ
հարցման հիման վրա տվյալների բազայում փոփոխություններ է անում։
Բոլոր ծրագրավորման լեզուները տվյալների բազային միանալու համար
տարբեր մեթոդներ են օգտագործում, բայց հարցում ուղարկելու պրինցիպը
նույնն է մնում, որովհետև բոլոր լեզուները օգտագործում են SQL.
Տվյալների բազայի տեսակներ են՝
- MySQL
- PostgreSQL
- Oracle
- SQLite
- MS SQL Server
Որոնք բոլորը օգտագործում են SQL լեզում հարցումների համար։
SQL-ը միջնորդ է հանդիսանում պրոեկտի և տվյալների բազայի միջև
SQL-ի միջոցովվ հրահանգներ ենք գրում, որի միջոցով կարող ենք տվյալների
բազայում ավելացնել, ջնջել, փոփոխել ինֆո;

Local server
PhpMyAdmin - ծրագիր է, GUI որի միջոցով աշխատում ենք MySQL տվյալների
բազայի հետ. Այս ծրագրի մեջ գրելում ենք SQL հրահանգներ, ստեղծում
աղյուսակներ, տվյալների բազաներ, տվյալների բազայի մեջ ինֆո
ավելացնում

Local server

XAMPP

OpenServer

MAMP

ՏԲ -ի ստեղծում
Ստեղծում են ՏԲ, table
CREATE DATABASE name;

DROP DATABASE name; - ջնջել ՏԲ


 SELECT - extracts data from a database
 UPDATE - updates data in a database
 DELETE - deletes data from a database
 INSERT INTO - inserts new data into a database
 CREATE DATABASE - creates a new database
 ALTER DATABASE - modifies a database
 CREATE TABLE - creates a new table
 ALTER TABLE - modifies a table
 DROP TABLE - deletes a table
 CREATE INDEX - creates an index (search key)
 DROP INDEX - deletes an index

Select/Select distinct
SELECT * FROM table_name; - select all table
SELECT column1, column2, ... FROM table_name; - select column/s from table
SELECT DISTINCT column1, column2, ...FROM table_name;
- select different values from table, we use it when column contains many duplicate
values and we want to get different values
SELECT COUNT(DISTINCT Country) FROM Customers; - These are lists the
number of different (distinct) customer countries

WHERE - used to filter records


It is used in SELECT, UPDATE, DELETE statements
SELECT column1, column2, ...FROM table_name WHERE condition;
SELECT Notes FROM Employees WHERE FirstName="Andrew";
Operators
= < > <= >=
<> or != not equal
BETWEEN
LIKE
IN - SELECT * FROM Customers
WHERE City IN ('Paris','London');

NOT
SELECT * FROM Customers
WHERE NOT City='Paris';
It can be used with and/or/= operators

NOT BETWEEN
Select customers with a customerID not between 10 and 60
SELECT * FROM Customers
WHERE CustomerID NOT BETWEEN 10 AND 60;

NOT IN

Select customers that are not from Paris or London


SELECT * FROM Customers
WHERE City NOT IN ('Paris', 'London');

NOT Greater Than/!>

SELECT * FROM Customers


WHERE NOT CustomerID > 50;
Or
SELECT * FROM Customers
WHERE CustomerID !> 50;
NOT Less Than/!<
SELECT * FROM Customers
WHERE NOT CustomerId < 50;

AND
The AND operator displays a record if all the conditions are TRUE.
SELECT * FROM Customers WHERE City = 'Berlin' AND PostalCode > 2000;

OR
The OR operator displays a record if any of the conditions are TRUE։
SELECT *
FROM Customers
WHERE Country = 'Germany' OR Country = 'Spain';

We can combine the AND and OR operators.


SELECT * FROM Customers WHERE Country = 'Spain' AND (CustomerName LIKE 'G
%' OR CustomerName LIKE 'R%');
Without (), it will return all customers from Spain that starts with a
"G", plus all customers that starts with an "R", regardless of the country value

ORDER BY Keyword
It is used to sort in ascending or descending order.
Default - ASC,
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

SELECT * FROM Employees ORDER BY BirthDate DESC

SELECT * FROM Customers


ORDER BY Country, CustomerName; - This means that it orders by Country, but if
some rows have the same Country, it orders them by CustomerName

SELECT * FROM Customers


ORDER BY Country ASC, CustomerName DESC; - sorted ascending by the "Country"
and descending by the "CustomerName" column

INSERT INTO
It is used to insert new records in a table.
1. INSERT INTO table_name
VALUES (value1, value2, value3, ...);
2. INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
3. INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...),
(value1, value2, value3, ...)
(value1, value2, value3, ...);

NULL Values
It is show empty columns
We can test NULL values with IS NULL and IS NOT NULL operators
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
Or

SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
UPDATE
It is used to changing existing values
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;

If you forget the WHERE clause, all records in the table will be updated!

DELETE
Delete rows
DELETE FROM table_name WHERE condition;
DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste';

We can delete all rows in a table without deleting the table


DELETE FROM table_name;

DROP TABLE
It for deleted table
DROP TABLE Customers;

TOP, LIMIT, FETCH FIRST, ROWNUM


Վերադարձնում ա նշված քանակով տողեր
TOP
SELECT TOP number|percent column_name(s)
FROM table_name
WHERE condition;
LIMIT
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;
FETCH FIRST
SELECT column_name(s)
FROM table_name
ORDER BY column_name(s)
FETCH FIRST number ROWS ONLY;
ROWNUM
SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number;

SELECT *
FROM (SELECT column_name(s) FROM table_name ORDER BY column_name(s))
WHERE ROWNUM <= number;

Functions
MIN() and MAX() Functions
SELECT MIN(column_name)
FROM table_name
WHERE condition;
To give the column a new name, use the AS keyword:
SELECT MIN(Price) AS SmallestPrice
FROM Products;

COUNT() Function
It returns the number of rows
SELECT COUNT(column_name)
FROM table_name
WHERE condition;

We can ignore duplicates by using the DISTINCT keyword in


the COUNT function.
SELECT COUNT(DISTINCT Price)
FROM Products;
To give the column a new name, use the AS keyword:
SELECT COUNT(*) AS [number of records]
FROM Products;

SUM() Function
It returns the total sum of a numeric column.
SELECT SUM(column_name)
FROM table_name
WHERE condition;
SELECT SUM(Quantity) AS total
FROM OrderDetails;
total amount
SELECT SUM(Price * Quantity)
FROM OrderDetails
LEFT JOIN Products ON OrderDetails.ProductID = Products.ProductID;

AVG() Function

It function returns the average value of a numeric column.

SELECT AVG(column_name)
FROM table_name
WHERE condition;
SELECT AVG(Price) AS [average price]
FROM Products;
Higher Than Average:
SELECT * FROM Products
WHERE price > (SELECT AVG(price) FROM Products);

LIKE Operator

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
Two symbols are used with Like operator
% represents zero, one, or multiple characters
_ represents one, single character

SELECT column1, column2, ...


FROM table_name
WHERE columnN LIKE pattern;

'a%' - starts with the letter "a"


'%and%' - contains the letter 'a'
'%a' - ends with 'a'
'b%s' - starts with "b" and ends with "s"
'Spain' - Without Wildcard

Return all customers from a city that starts SELECT * FROM Customers
with 'L' followed by one wildcard character, WHERE city LIKE 'L_nd__';
then 'nd' and then two wildcard
characters:

Return all customers that starts with "a" SELECT * FROM Customers
and are at least 3 characters in length: WHERE CustomerName LIKE 'a__%';
Return all customers that have "r" in the SELECT * FROM Customers
second position: WHERE CustomerName LIKE '_r%';

We can use AND or OR operators SELECT * FROM Customers


WHERE CustomerName LIKE 'a%' OR Custom
erName LIKE 'b%';

We can use NOT operator SELECT * FROM Customers


WHERE CustomerName NOT LIKE 'a%';

Symbo Description
l

% or * Represents zero or more characters

_ or ? Represents a single character

[] Represents any single character within the brackets

^ or ! Represents any character not in the brackets

- Represents any single character within the specified range

{} Represents any escaped character

# Represents any single numeric character

Wildcard Characters

Select all records where the first letter of SELECT * FROM Customers
the City is an "a" or a "c" or an "s". WHERE City LIKE '[acs]%';

Select all records where the first letter of SELECT * FROM Customers
the City starts with anything from an "a" to WHERE City LIKE '[a-f]%';
an "f".

Select all records where the first letter of SELECT * FROM Customers
the City is NOT an "a" or a "c" or an "f". WHERE City LIKE '[!acf%';
IN Operator

The IN operator allows you to specify multiple values in a WHERE clause.


It replaces OR operator.

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

Return all customers from 'Germany', SELECT * FROM Customers


'France', or 'UK' WHERE Country IN ('Germany', 'France', 'UK');

NOT IN

Return all customers that are NOT SELECT * FROM Customers


from 'Germany', 'France', ot 'UK': WHERE Country NOT IN ('Germany', 'France', '
UK');

IN (SELECT)/NOT IN (SELECT)

Return all customers that have an SELECT * FROM Customers


order in the Orders table: WHERE CustomerID IN (SELECT CustomerID
FROM Orders);

BETWEEN/NOT BETWEEN Operator

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

You might also like