Clauses, Operators Flashcards

(56 cards)

1
Q

DISTINCT

A

Removes duplicates. Ensures that each value only shows up once in the results.

SELECT DISTINCT id
FROM customers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

TOP

A

Limits the data; restricts the number of rows

SELECT TOP 3 *
FROM customers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

HAVING vs WHERE clause

A

If you use the HAVING clause, you need to filter data after aggregation.

If you use WHERE clause, the aggregation does not need to occur first

Example:
Find the avg score for each country, considering only customers with a score not equal to zero, and return only those countries with an avg score greater than 430.

SELECT
country,
avg (score) AS avg_score
FROM customers
WHERE score <> 0
GROUP BY country
HAVING avg (score) > 430

SQL pulls countries
THEN scores that are not equal to zero
THEN groups by country
THEN calculates the avg score
THEN filters out the scores that are greater than 430

*If didn’t use HAVING in above example could get the wrong results.
It would filter out any scores that were greater than 430 and THEN calculate avg.
Want it to calculate avg and THEN filter out scores greater than 430.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

SQL Aggregate Functions

A

A function that performs a calculation on a set of values and returns a single value.

MIN (): smallest value in selected col
MAX (): largest value in selected col
COUNT (): number of rows in a set
SUM (): Total sum of a numerical col
AVG (): Avg value of numerical col

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

SQL arithmetic Operators

A

+ Add
- Subtract
* Multiply
/ Divide
% Modulo

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

SQL Comparison Operators

A

= Equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than equal to
<> Not equal to

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

WHERE

A

Filters data based on a condition

Syntax:

SELECT
country
SUM (score)
FROM table
WHERE score>400
GROUP BY country
HAVING SUM (score)>800

NOTE: WHERE goes between FROM and GROUP BY

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

HAVING

A

Filters aggregated data

Syntax:

SELECT
country,
SUM (score)
FROM table
GROUP BY country
HAVING condition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

GROUP BY

A

Aggregates data (i.e., combines 2 columns).

E.g., adds up scores for each country.

Ex//
SELECT country, SUM (score) AS Total_Score
FROM customers
GROUP BY country

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

ORDER BY

A

Sorts data. Use ASC (lowest to highest: 1, 2, 3) or DESC (highest to lowest: 5, 4, 3, 2, 1).
Default if not specified is standardly ASC.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

COUNT

A

Count total number of something (e.g., to count the total number of customers).

SELECT COUNT (id)
FROM customers
WHERE condition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Coding Order of Queries

A

SELECT DISTINCT TOP 2
Col1,
SUM(col2)
FROM table
WHERE col=10
GROUP BY col1
HAVING SUM(col2)>30
ORDER BY col1 ASC

If you want to filter data before aggregation:
SELECT DISTINCT TOP 2
Col1,
Col2
FROM table
WHERE col=10

If you want to group the data:
SELECT DISTINCT TOP 2
Col1,
SUM(col2)
FROM table
WHERE col=10
GROUP BY col1

If you want to filter the data after aggregation:
SELECT DISTINCT TOP 2
Col1,
SUM(col2)
FROM table
WHERE col=10
GROUP BY col1
HAVING SUM(col2)>30
ORDER BY col1 ASC

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

INT

A

Integar
Only contains numbers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

VARCHAR

A

String/text
Or numbers
Or symbols

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Primary key

A

Every table has this–necessary to have one unique identifier (e.g., a customer ID for each customer).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

UPDATE

A

DML
Change the content of already existing rows.

UPDATE customers
SET score
WHERE id = 6

*WARNING: if you execute the query without a WHERE, all rows will be updated.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

INSERT

A

DML

Manipulate data (i.e., insert new rows or update rows)

2 methods: manually OR using a table

INSERT INTO table_name (col1, col2,col3)
VALUES (value1,value2,value3)

Columns: cols that data will be entered into
Values: data that will be entered

NOTE: if you don’t specify the cols, SQL will expect you to insert values into each col. *Sometimes you’ll just eant to insert values into a few cols (e.g., col1, col3, col7). In that case, you need to specify the cols.

RULE: match the number of cols and values.

Ex//
INSERT INTO customers (id, first name, country, score)
VALUES
(6, ‘Anna’, ‘USA’, NULL)

Id=6, first name=Anna, country=USA, score=NULL

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What operator(s) should you use with NULL?

A

Always use IS NULL or IS NOT NULL

Ex//
SELECT *
FROM customers
WHERE score = NULL

*Above query will return no results, even when there is a NULL value

BUT below query will return results (if a NULL value is present).

SELECT *
FROM customers
WHERE score IS NULL

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

TRUNCATE

A

DML

Use if you want to delete everything from a table.
It is faster than using DELETE; use TRUNCATE when working with large tables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Not SQL BUT order of operations in math

A

PEMDAS

Parentheses
Exponents
Multiplication
Division
Addition
Subtraction

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

SQL Logical Operators

A

AND: all conditions must be true
OR: At least one condition must be true
NOT: Excluded matching values
BETWEEN: Check if a value is within a range
IN: Check if a value exists in a list
LIKE: Search for a pattern in a text

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

BETWEEN

A

SQL logical operator

Syntax:
WHERE (col name) BETWEEN (value) AND (value)

*NOTE: BETWEEN is inclusive so if say between 100 and 500, the numbers 100 and 500 will be included.

If you don’t want to include either 100 or 500:
WHERE score >= 100 AND score <=500

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

SQL comparison operators

A

= equal to
<> or =! Not equal to
X > greater than
X >= greater than or equal to
X< less than
X<= less than or equal to

24
Q

SQL logical operators

25
SQL range operator
BETWEEN
26
SQL membership operator
IN NOT IN
27
IN
SQL membership operator SELECT * FROM customers WHERE country IN ('Germany', 'USA'); Above query is ideal when you have many values (i.e., Germany, USA, etc.). Could also do the following if have only a few values: SELECT * FROM customers WHERE country = 'Germany' or country = 'USA'
28
%
LIKE M% Maria (name starts with M) Ma M___ %r% (r anywhere in name) Maria Peter Rayn R__
29
_
_ _ b % Albert Rob Abel
30
Database SQL DBMS Server
Database: container where the data lives SQL: structured query language DBMS: Database Management System. The software that manages all the requests put through the database. Requests may include: people using SQL to request info from the database, applications using SQL to request info from the database, SQL requests to create data visuals from the database, etc. Server: physical machine where the database lives
31
Types of databases
Relational Key-value Column-based Graph Document
32
Relational Database AKA SQL databse
Spreadsheets with relationships linking them together Microsoft SQLServer MySQL, PostgreSQL
33
Key-Value Database
Pairs of keys and values Think of a dictionary where you have a word (key) and definition (value)
34
Column-based database
Group data by columns (as opposed to row). This is an advanced database that handles large quantities of data. Its main purpose is to search for data.
35
Graph Database
Relationship between objects. Main idea: how to connect my fata points?
36
Document Database
Data stored as entire documents. The structure of the data is less important.
37
SQL database
38
No SQL databases
Types of databases in this category: -key-value (Redis, Amazon Dynamo DB) -column-based (Apache Cassandra, Amazon Redshift) -Graph (Neo4j) -Document (MangoDB)
39
Servers>databases>schema>tables>data types
Servers (i.e., the physical machine where database(s) live) can host multiple databases (i.e., containers of your data)> Each database can have multiple schemas (i.e., a logical container that can group related objects) Ex//you have hundreds of tables & put all tables related with orders in one schema and all tables related to customer info in another >Each schema can have multiple objects (e.g., tables) >Each table organized data Column=field Row=record Every table has a primary key. Must have a unique identifier (e.g., each customer has a unique ID like 1 or 2 or 3) >Each table has cells with singular values Value types: Numeric: integer (1, 2, 30) or Decimal (3.14, 100.50) String/text: character or VARCHAR -character: fixed (e.g., if you define it as 5 characters, it will reserve 5 characters from the space) -VARCHAR: use when you want things to be more dynamic -Date &Time 2015-10-30 or 09:30:00
40
Servers
(i.e., the physical machine where database(s) live) can host multiple databases (i.e., containers of your data)
41
Schema
Each database can have multiple schemas (i.e., a logical container that can group related objects) Ex//you have hundreds of tables & put all tables related with orders in one schema and all tables related to customer info in another >Each schema can have multiple objects (e.g., tables)
42
Objects
>Each schema can have multiple objects (e.g., tables)
43
Table
Each table has organized data Column=field Row=record Every table has a primary key. Must have a unique identifier (e.g., each customer has a unique ID like 1 or 2 or 3)
44
Field is another name for
Column=field
45
Record is another name for
Row=record
46
Primary key
Every table has a primary key. Must have a unique identifier (e.g., each customer has a unique ID like 1 or 2 or 3)
47
Values & value types
>Each table has cells with singular values Value types: Numeric: integer (1, 2, 30) or Decimal (3.14, 100.50) String/text: character or VARCHAR -character: fixed (e.g., if you define it as 5 characters, it will reserve 5 characters from the space) -VARCHAR: use when you want things to be more dynamic -Date &Time 2015-10-30 or 09:30:00
48
DDL
Data Definition Language Used to define the way data is structured in a database. DDL consists of SQL commands that define and modify database structures. It sets up structures like tables and schemas, modifies their design, and removes them when they're no longer needed DDL commands define tables, indexes, and relationships. These commands modify structures and remove database objects. Create: defines new database objects like tables, indexes, or entire databases. --CREATE DATABASE --CREATE TABLE --CREATE INDEX Alter: modifies existing database structures without affecting stored data. --ALTER TABLE Drop: deletes database objects permanently --DROP TABLE users --DROP INDEX --TRUNCATE
49
Create
DDL commands that define new database objects like tables, indexes, or entire databases. --CREATE DATABASE --CREATE TABLE --CREATE INDEX
50
Alter
DDL commands that modify existing database structures without affecting stored data. --ALTER TABLE
51
Drop
DDL commands that delete database objects permanently --DROP TABLE users --DROP INDEX --TRUNCATE
52
DML
Data Manipulation Language Used to manage data within a database. SQL used to insert, retrieve, modify, and delete records within a database structure. --INSERT --UPDATE --DELETE --SELECT
53
DQL
Data Query Language Responsible for reading/querying data from a database (In SQL, this corresponds to SELECT)
54
DCL
Data Control Language Responsible for the administrative tasks of controlling the database itself (e.g., granting and revoking database permissions for users).
55
What do DDL, DML, DQL, and DCL do?
DDL: data definition language. Defines database objects like tables and indexes DML: data manipulation language. Manages data within those structures DQL: data query language. Queries data from a database. DCL: data control language. Responsible for admin tasks related to the database (i.e., granting and revoking database permissions for users.)
56
When to use joins?
1. Recombine data Ex// you have customer data in a bunch of diff tables and want to combine it in one table. 2. Data Enrichment You have a customer table and want to add data from one other table (e.g., zipcode) 3. Check Existence/Filtering You are running queries in the customer table and need to filter your results based on whether the customers ordered an item. This data is in the orders table. You filter your final results based on that