SQL

0.0(0)
Studied by 1 person
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/65

encourage image

There's no tags or description

Looks like no tags are added yet.

66 Terms

1
New cards

What are the 2 core functions SQL consists of?

  • Data Definition Language (DDL)

    • defining + creating database objects (e.g. tables,relationships,data types,constraints)

  • Data Manipulation Language (DML)

    • manipulating + querying data in the db (populating,reading,updating data)


2
New cards

What are literals?

constant values

3
New cards

What must non-numeric literals be enclosed in?

single quotes

<p>single quotes</p>
4
New cards

Are numeric literals enclosed in quotes?

no

<p>no</p>
5
New cards

What are the data types that can be used for a character?

  • CHAR (length) - fixed length

  • VARCHAR (length) - variable length (max)

  • TEXT - variable length


6
New cards

What are the data types that can be used for a numeric value?

  • INTEGER, SMALLINT - integer

  • OID - obejct identifier

  • NUMERIC(precision,decimal) - number

  • FLOAT, DOUBLE - floating point number


7
New cards

What are the data types that can be used for a temporal value?

  • DATE - date

  • TIME - time

  • TIMESTAMP - date & time

  • INTERVAL - interval of time


8
New cards

What is the data type that can be used for a logical value?

BOOLEAN - boolean (e.g. true/fale, yes/no)

9
New cards

Why should you never use a FLOAT or DOUBLE data type for money?

  • approximate (inexact) types

  • may result in discrepancies - rounding errors


10
New cards

What data type should you use for money?

Numeric OR DBMS’s monetary type

11
New cards

How do you delete database objects in SQL?

DROP (delete child objects first & parent objects last)

<p>DROP (delete child objects first &amp; parent objects last)</p>
12
New cards

How do you create a table in SQL?

CREATE TABLE (create parent tables before child tables)

<p>CREATE TABLE (create parent tables before child tables)</p>
13
New cards

What do constraints in SQL prevent?

  • accidental damage

  • changes that may cause data inconsistency


14
New cards

Where are constraints specified in SQL?

specified using clauses in CREATE TABLE statement

OR

specified using database procedures/rules

15
New cards

What are the type of SQL constraints?

  • Mandatory Data - NOT NULL

  • Entity Integrity - PRIMARY KEY

  • Referential Integrity - FOREIGN KEY

  • Column value constraints - CHECK,UNIQUE,DEFAULT

  • Enterprise constraints


16
New cards

How would you write an SQL constraint?

knowt flashcard image
17
New cards

How would you write a check SQL constraint?

knowt flashcard image
18
New cards

How do you create a table that has a composite key?

  • create the key after column definition


<ul><li><p>create the key after column definition</p></li></ul><p></p>
19
New cards

What is the referential integrity rule?

value of FK must match existing PK it refers to/be null

20
New cards

When is deletion of a row in a “parent” table not allowed?

if there exists any referenced rows in the “child” table

21
New cards

What happens if the PK in the parent table is deleted/changed?

effects are dependent on actions specified in REFERENCES clause of child table

22
New cards

What are the referential integrity options if a record is deleted in the parent table?

  • CASCADE

  • SET NULL

  • SET DEFAULT


23
New cards

What does “CASCADE” do?

auto delets all referenced records in the child table

<p>auto delets all referenced records in the child table</p>
24
New cards

What does “SET NULL” do?

sets FK value of all referenced child records to NULL (default option)

25
New cards

What does “SET DEFAULT” do?

auto sets FK of all referenced child records to default value

<p>auto sets FK of all referenced child records to default value</p>
26
New cards

How do you add a FK in SQL?

knowt flashcard image
27
New cards

What is altering tables used for?

to make changes to columns in existing table

28
New cards

How do you add a new column to a table in SQL?

knowt flashcard image
29
New cards

How do you add a delete an existing column from a table in SQL?

knowt flashcard image
30
New cards

How do you modify an existing column from a table in SQL?

knowt flashcard image
31
New cards


32
New cards

What are the 4 statements for data manipulation in SQL known as CRUD?

  • (CREATE) INSERT - add data

  • (READ) SELECT - retrieve data

  • (UPDATE) UPDATE - modify data

  • (DELETE) DELETE - delete data


33
New cards

What are the additional clauses when using the SELECT statement?

knowt flashcard image
34
New cards

How do you select all columns from a table?

knowt flashcard image
35
New cards

How do you select specific column(s) from a table?

knowt flashcard image
36
New cards

How do you select distinct (no duplicates) values of a column?

knowt flashcard image
37
New cards

Give an example of how a SELECT statement can include column expressions to perform calculations

knowt flashcard image
38
New cards

What is the “WHERE” clause used to retrieve?

rows that match a specified filter condition

<p>rows that match a specified filter condition</p>
39
New cards

What are the 5 basic conditions of the “WHERE” clause?

  • Comparison - compare values of expression

  • Range - does value of expression fall into range?

  • Set membership - does value = 1 set of specified values?

  • Pattern match - does string value match specified pattern?

  • Null - does column have a null value?


40
New cards

What are operators that can be used in the WHERE clause

  • = equals

  • <> not equal

  • > greater than

  • < less than

  • >= greater than or equal to

  • BETWEEN between inclusive range

  • IS NULL column expression value is null

  • LIKE

  • IN


<ul><li><p>= equals</p></li><li><p>&lt;&gt; not equal</p></li><li><p>&gt; greater than</p></li><li><p>&lt; less than</p></li><li><p>&gt;= greater than or equal to</p></li><li><p>BETWEEN between inclusive range</p></li><li><p>IS NULL column expression value is null</p></li><li><p>LIKE</p></li><li><p>IN</p></li></ul><p></p>
41
New cards

What are the 3 “WHERE” clause compund conditions?

  • AND = both 1st & 2nd condition true

  • OR = 1st or 2nd condition true

  • NOT = negates a condition


42
New cards

How would you test if something is null in sql?

knowt flashcard image
43
New cards

How would you test if something is not null in sql?

knowt flashcard image
44
New cards

What is the “order by” clause used for?

to sort query results

45
New cards

How would you order query results in ascending order in SQL?

knowt flashcard image
46
New cards

How would you order query results in descending order in SQL?

knowt flashcard image
47
New cards

What is the “limit” clause used for?

to limit the number of rows returned by a query to a max value

48
New cards

How would you use a “LIMIT” clause in SQL?

knowt flashcard image
49
New cards

What is meant by aggregating data?

combining data & derive summary info (e.g. sums,counts,averages, etc.)

50
New cards

What are the 5 aggregate functions defined in SQL?

  • COUNT - return num of values (in specified column)

  • SUM - returns sum of values (in specified column)

  • AVG - returns average of values (in specified column)

  • MIN - returns smallest value (in specified column)

  • MAX - returns largest value (in specified column)


51
New cards

What is the syntax for aggregate functions?

knowt flashcard image
52
New cards

Which of the 5 aggregate fucntions defined in SQL applies to both numeric and non-numeric data?

COUNT, MIN, MAX

53
New cards

Which of the 5 aggregate fucntions defined in SQL applies to numeric data?

SUM, AVG

54
New cards

What does “COUNT(*)” do?

counts all the rows of a table regardless of nulls/duplicates

55
New cards

What is the “GROUP BY” clause used for?

  • to get sub-totals/summary data (aggregates) by groups

  • specifies attribute(s) to group records by


<ul><li><p>to get sub-totals/summary data (aggregates) by groups</p></li><li><p>specifies attribute(s) to group records by</p></li></ul><p></p>
56
New cards

What is the rule on what must be included when using the GROUP BY clause?

  • each item in SELECT list must be single-vaued per group

  • results of group by can only contain:

    • grouping attributes

    • constants

    • aggregate function results

  • (every column listed in the SELECT statment has to be associated w/ an aggregate function OR it has to appear in the GROUP BY clause)


57
New cards

What does the “HAVING” clause do?

used w/ GROUP BY to reduce groups that appear in the query result (filters groups)

<p>used w/ GROUP BY to reduce groups that appear in the query result (filters groups)</p>
58
New cards

What are the 2 ways to remove ambiguity in a multi-table query result? (e.g. if columns have same name but diff table)

  • prefix attribute w/ required table name

    • e.g. Staff.Staff_ID

  • use aliases to simplify/shorten length of query

    • e.g. s.Staff_ID (‘s’ alias for staff)


59
New cards

What are the steps when querying multiple tables?

  • what tables needed

    • idenitfy + give aliases to each table

  • what columns needed for each column

  • what are matching columns in tables

    • specify your JOIN condition

  • what type of JOIN is needed

  • are any other clauses required


60
New cards

What does an inner join return?

rows that have a match in both tables

<p>rows that have a match in both tables</p>
61
New cards

What does a left outer join return?

  • all rows from the left (1st) table & matched from the right table

(NULLs returned for columns in right side table when there is no match)


<ul><li><p>all rows from the left (1st) table &amp; matched from the right table</p></li></ul><p>(NULLs returned for columns in right side table when there is no match)</p><p></p>
62
New cards

What does a right outer join return?

  • all rows from right table & macthed from left table

(NULLs returned for columns in left side table when there is no match)


<ul><li><p>all rows from right table &amp; macthed from left table</p></li></ul><p>(NULLs returned for columns in left side table when there is no match)</p><p></p>
63
New cards

What does a full join return?

all rows from both tables whether there’s a match or not

64
New cards

What does a self join return?

rows w/ a match in the same/different columns of the same table

65
New cards

Illustrated types of joins

knowt flashcard image
66
New cards