Basic SQL statements

The SQL language standard was adopted in 1992 and is still in use. It was he who became the standard for many database management systems. Of course, some manufacturers use their own interpretations of the standard. But in any system there are still the main components - SQL statements.

Introduction

Using SQL statements in databases , values, tables are managed and retrieved for further analysis and display. They are a set of keywords by which the system understands what to do with the data.

sql statements




Several categories of SQL statements are defined:

  • definition of database objects;
  • value manipulation;
  • protection and management;
  • Session parameters
  • database information;
  • static SQL
  • dynamic SQL.

SQL statements for data manipulation

, .

sql query statements




INSERT. . , , . :





INSERT INTO

( 1, 2)

VALUES ( 1, 2).

SQL INSERT , :

INSERT INTO

1 ( 1, 2)

SELECT 1, 2

FROM 2

WHERE 2. 1>2

2, 2 1 .

UPDATE. , SQL .

:

UPDATE 1

SET 2 = «»

WHERE 1. 1 = 1

, 1 .

DELETE. . - .

DELETE FROM

WHERE . 1 = 1

. :

DELETE FROM .

SELECT. , .

SELECT

SELECT — . . MS SQL SELECT . .

«*».





SELECT *

FROM 1

1.

WHERE, 1 , 2 1.

SELECT *

FROM 1

WHERE 1. 1 > 2

, .

SELECT 1. 1

FROM 1

, 1. MS SQL , , .

ms sql statements




SELECT

1. 1

1. 2

1. 3

«=» AS EQ

1. 2 * 1. 3 AS SUMMA

FROM 1

, 1, EQ SUMMA. «+», 2 3. , :

1

2

3

EQ

SUMMA

1

10

50

+

500

2

15

100

+

1500

SELECT, - . ORDER BY.

SELECT

1. 1

1. 2

1. 3

FROM 1

ORDER BY 2

:

1

2

3

1

1

54

3

2

12

7

3

100

2

5

1

, 2 .

. , , :

«»

1

2

«»

1

1

10000

2

0,5

3500

, - . SQL :

SELECT

.

.

.

.

FROM ,

WHERE . = .

, . :

1

1

10000

2

0,5

3500

SELECT.

SQL SELECT . .

core sql statements




, «», :

SELECT COUNT (*) AS N

FROM

.

N

2

, , , . SUM, MAX, MIN, AVG.

, «»:

1

1

10000

2

0,5

3500

:

SELECT

SUM(.) AS SUMMA

MAX(.) AS MAX

MIN(.) AS MIN

AVG(.) AS SRED

FROM

:

SUMMA

MAX

MIN

SRED

13500

10000

3500

6750

, , .

,

SQL

SELECT .

FROM

WHERE . = 1

UNION

SELECT .

FROM ,

WHERE . = 1

, . .

SELECT

SELECT , . FROM. , .

SQL WHERE. SELECT .

sql comparison operators




GROUP BY, .

. SQL .

  • «=». , , . , - WHERE . = 1.

  • «>». . , TRUE .

  • «<». . .

  • «<=» «>=». , , .

  • «<>». . TRUE, . - «!=».

LIKE

«». LIKE SQL — . .

like statement in sql




, : «» , «». :

SELECT *

FROM

WHERE LIKE `%`

, . «» SQL .

CASE

SQL Server . switch . CASE SQL .

, «» .

1

1

10000

2

0,5

3500

:

SELECT *

FROM

WHERE CASE WHEN SELECT MAX() THEN

WHEN SELECT MIN() THEN

END

«». END «», «» «» .

, SQL CASE — COALESCE.

— , , .

, — CREATE TABLE. , . CREATE TABLE, , .

, «» :

CREATE TABLE

( number(10) NOT NULL

varchar(50) NOT NULL

varchar(50) NOT NULL)

, , NULL.

DROP TABLE

— . IF EXISTS. , . :

DROP TABLE IF EXISTS.

CREATE INDEX

SQL , . , , . :

CREATE INDEX _

ON _(_)

T-SQL, Oracle, PL SQL .

ALTER TABLE

, . , . Oracle SQL, Postgres .

ALTER TABLE.

  • ADD. . : ALTER TABLE _ ADD _ __. IF NOT EXISTS, , ;

  • DROP. . IF EXISTS, , , ;

  • CHANGE. . : ALTER TABLE _ CHANGE _ _;

  • MODIFY. . : ALTER TABLE _ MODIFY _ _ ;

CREATE VIEW

SQL , . , . SQL SELECT. , , .

t sql statement




:

CREATE VIEW AS SELECT FROM *

, .

SQL , . , .

  • COUNT. . , . SELECT COUNT * FROM ;

  • AVG. . ;

  • MIN MAX. . ;

  • SUM. — . . DISTINCT, ;

  • ROUND. . ;

  • LEN. , . ;

  • NOW. .

SQL , , .

  • AS. , , .

  • BETWEEN. . , . ;.

  • NOT. .

  • TRUNCATE. . , . , SQL . TRUNCATE, .

  • LIMIT. . , . . , . , .

  • UNION. . . , UNION . : SELECT _ FROM _ UNION SELECT __ FROM _ . .

  • PRIMARY KEY. « ». , . . , , , .

  • DEFAULT. , , . , .

SQL

  1. NULL. NULL. , . , , NULL .

  2. . , . , «» . .

  3. . , LIMIT TOP. .

  4. . . ? . .

  5. . , , . .

  6. . , . .

  7. . , . . , .

  8. . . «» , . , , 255, 4- INT, TINYINT 1 .

, SQL — , -, , . SQL .

. , PL SQL , SQL Server. , .

In the future, analogues that could surpass SQL in functionality and performance are unlikely to appear, so this area is a fairly promising niche for any programmer.




All Articles