SQL Queries Flashcards


Set Details Share
created 8 months ago by adas083
3 views
Basic SQL Queries
updated 8 months ago by adas083
show moreless
Page to share:
Embed this setcancel
COPY
code changes based on your size selection
Size:
X
Show:

1

SELECT

select a column from a table

* for all columns

2

DISTINCT

Only unique values

3

ORDER BY

Put things in order (ascending, descending)

4

WHERE

Search with a condition/filter, like an IF

5

INSERT

Adds new row to table

  • INSERT INTO table (col1, col2, …)
  • VALUES (val1, val2, …)

6

UPDATE

Update data of existing rows in table, you can use SET to change the actual value and WHERE for the condition

  • Ex: Someone got married, change their last name
    • UPDATE employees
    • SET lastname = 'Smith'
    • WHERE employeeid = 3;

7

DELETE

Delete a row FROM table WHERE condition

8

GROUP BY

rows that have the same values into summary rows, use with something like COUNT

9

JOIN

Combining two tables

10

LEFT JOIN

All rows from Left table are included in set, fills entries without data with NULL

11

RIGHT JOIN

All rows in Right table are included in set, fills entries without data with NULL

12

INNER JOIN

All rows in only both tables are included in set

13

OUTER/FULL JOIN

Combines all tables, kind of creates a mess

14

NULL

No value

15

SELECT TOP

Shows the top n results, same as LIMIT n

16

List of (Statistical) Functions

  • SUM
  • AVG
  • MAX
  • MIN
  • COUNT

17

What is an Index?

Speed up getting data from tables

Assigning an index number to a set of values (column) in a table

  • Ex:
    • CREATE [UNIQUE] INDEX index_name
    • ON table (column)

18

Transactions

Group multiple SQL statements into a single logical unit of work that either succeeds or fails as a whole.

  • BEGIN TRANSACTION;
  • SQL queries
  • COMMIT

19

Views

Virtual tables stored by a SELECT statement, stored queries

  • CREATE VIEW

20

Stored Procedures

Stored procedures are a set of SQL statements that are stored in the database and can be called by applications,

like a script/transactions?

21

Normalization

Normalization is the process of organizing data in a database so that it is structured according to a set of rules that eliminate redundancy and improve data consistency.

22

LIKE

Used with WHERE to find patterns in a query with a wildcard

23

Wildcards

% Represents zero or more characters

_ Represents a single character

Like Regex

24

IN

Used to specify values in a WHERE clause, a lot like OR

25

BETWEEN

query in a range of values

26

Aliases

Give a table/column a temporary name, uses AS

27

UNION

returns distinct values SELECTed from both tables

28

HAVING

WHERE but for aggregate functions (COUNT)

29

EXISTS

test for the existence of a record in a subquery

30

ANY

Returns boolean if any one record/value matches the subquery

31

ALL

Returns boolean if all records/values matches the subquery

32

SELECT INTO

copies data from one table into a new table

33

INSERT INTO SELECT

copies data from one table and inserts it into another table

34

CASE

Uses WHEN - THEN conditions and ELSE as a default

35

Null functions

IFNULL(), ISNULL(), COALESCE(), and NVL()

36

COALESCE

returns the first non-null value in a list

37

NVL

Oracle version of ISNULL

38

Comment

-- single line

/* */ multiline

39

Operators

All the same as Python basically

40

CREATE DATABASE, DROP DATABASE

creates a new SQL database, drops SQL database

41

BACKUP DATABASE

create a full back up of an existing SQL database

42

CREATE TABLE, DROP TABLE

create new table, drops table

43

ALTER TABLE

add, delete, or modify columns in an existing table

Ex: ALTER TABLE table_name ADD column_name

44

Constraints

Used with CREATE TABLE or ALTER TABLE to specify some rules

45

List of constraints

  • NOT NULL
  • UNIQUE
  • PRIMARY KEY
  • FOREIGN KEY
  • CHECK
  • DEFAULT
  • INDEX/CREATE INDEX

46

NOT NULL

Ensures that a column cannot have a NULL value

47

UNIQUE

Ensures that all values in a column are different

48

PRIMARY KEY

A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table

49

FOREIGN KEY

Prevents actions that would destroy links between tables

50

CHECK

Ensures that the values in a column satisfies a specific condition

51

DEFAULT

Sets a default value for a column if no value is specified

52

CREATE INDEX/INDEX

Used to create and retrieve data from the database very quickly

53

AUTO INCREMENT

allows a unique number to be generated automatically when a new record is inserted into a table

54

Dates

YYYY-MM-DD (HH:MM:SS)

55

Injection

Can be used for hacking? Ex: OR 1=1

56

Hosting DBs

MS SQL Server, Oracle, MySQL, and MS Access

57

Data types

char, varchar = string, int, smallint, tinyint, bigint, BLOB (big object)