Database Systems - SQL Syntax

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/38

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

39 Terms

1
New cards

Data Types

  • INT,

  • VARCHAR(1) [specify length],

  • DATE [YYYY-MM-DD],

  • TIMESTAMP [YYYY-MM-DD HH:MM:SS]

2
New cards

Constraints

  • NOT NULL [Ensures that a column cannot have a NULL value]

  • UNIQUE [Ensures that all values in a column are different]

  • PRIMARY KEY [Uniquely identifies each row in a table]

  • FOREIGN KEY [Prevents actions that would destroy links between tables]

  • DEFAULT [Sets a default value for a column if no value is specified]

  • CREATE INDEX [Used to create and retrieve data from the database very quickly]

3
New cards

Create new table

CREATE TABLE table_name (
    column1 datatype,
    column2 datatype,
    column3 datatype
);

4
New cards

Add new column

ALTER TABLE table_name
ADD column_name datatype;

5
New cards

Insert into / Add new row …

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

INSERT INTO table_name
VALUES (value1, value2, value3, ...);

6
New cards

Update, change, modify…

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

7
New cards

Delete table

DROP TABLE table_name;

8
New cards

Delete all rows from table

DELETE FROM table_name;

9
New cards

Delete specific row from table

DELETE FROM table_name

WHERE condition;

10
New cards

Delete column from table

ALTER TABLE tablename
DROP COLUMN columnname;

11
New cards

Display… / List …

SELECT column1, column2, ...
FROM table_name;

12
New cards

Display the different …

SELECT DISTINCT column1, column2, ...
FROM table_name;

13
New cards

Display __ who / that / from …

SELECT column1, column2, ...
FROM table_name
WHERE condition;

14
New cards

Sort in ascending / descending order

SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

[alphabetically-ASC /

reverse alphabetically-DESC]

15
New cards

AND Operator

SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

16
New cards

Combined AND and OR

SELECT * FROM table_name
WHERE condition1 AND ( condition2 OR condition3 );

17
New cards

OR Operator

SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

18
New cards

NOT Operator

SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;

SELECT *
FROM table_name
WHERE column1 NOT IN / BETWEEN | NOT ;

19
New cards

NULL | Missing data… / not missing data…

SELECT column_names
FROM table_name
WHERE column_name IS NULL;

SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;

20
New cards

Select top …

SELECT TOP number|percent column_name(s)
FROM table_name
WHERE condition;

ORDER BY column1, column2, … ASC/DESC

21
New cards

Display minimum, lowest, smallest, earliest…

SELECT MIN(column_name)
FROM table_name
WHERE condition;

22
New cards

Display maximum, largest, latest…

SELECT MAX(column_name)
FROM table_name
WHERE condition;

23
New cards

Number of… | Number of distinct…

SELECT COUNT/COUNT(DISTINCT column_name)
FROM table_name
WHERE condition;

24
New cards

Display the Sum… , Total amount…

SELECT SUM(column_name)
FROM table_name
WHERE condition;

25
New cards

Display the Average

SELECT AVG(column_name)
FROM table_name
WHERE condition;

26
New cards

Starts with…

SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE ‘A%’;

27
New cards

Ends with…

SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE %A’;

28
New cards

Contains ‘xyz’ …

SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE %A%’;

29
New cards

Wildcard characters

SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE ‘A_bc’;

30
New cards

Multiple OR conditions

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

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

31
New cards

Display between range/ outside range…

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

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

32
New cards

Aliases

SELECT column_name AS alias_name
FROM table_name;

[If the alias contains spaces]:

SELECT column_name AS [alias_name]
FROM table_name;

SELECT column1 + ‘ ‘ + column2 AS alias_name
FROM table_name AS alias_name;

33
New cards

Inner JOIN : Display _ and their …

SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name = table2.column_name;

[Matching values on both tables]

34
New cards

Left / Right JOIN : Display _ even if they have no …

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;

SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;

35
New cards

Display _ who have never / have no …

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name

WHERE table2.othercolumn_name IS NULL;

[Displays only unmatched rows from join]

36
New cards

Outer JOIN : Combine every record from tables…

SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;

37
New cards

Self JOIN : Display _ that are from the same …

SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;

38
New cards

UNION : Display _ from both tables

SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;

[Union selects only distinct values]

39
New cards

Display per … , grouped based on …, by each…

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);