Common SQL Commands & SQL Select Statements

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

1/54

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.

55 Terms

1
New cards

SELECT

Retrieves specific data from a database

2
New cards

UPDATE

Modifies existing data w/in a table.

3
New cards

DELETES

Removes specific records from database

4
New cards

INSERT INTO

Adds new rows into a table

5
New cards

CREATE DATABASE

Establishes a new database from scratch

6
New cards

ALTER DATABASE

Adjusts database' settings or attributes (fields/columns)

7
New cards

CREATE TABLE

Defines a new table w/ columns & data types

8
New cards

ALTER TABLE

  • Changes the structure of a table

  • For e.g:

  • Adding/removing a column

  • Changing a column’s data type

  • Renaming a table or column

  • Adding/dropping constraints

- Such as adding, renaming, or removing columns

9
New cards

DROP TABLE

- Permanently deletes a table & all of its data (including its structure)

- Cannot be undone (no rollback allowed)

10
New cards

CREATE INDEX

  • Improves speed of data retrieval operations on a table

    • does this by creating pointers to data based on 1 or more columns

  • Doesn’t affect the data itself—it’s purely a performance booster.

11
New cards

JOIN

Combines records from two or more tables based on a common column.

12
New cards

TRUNCATE TABLE

Removes all records (data) from a table, but keeps its structure intact.

13
New cards

DISTINCT

Retrieves unique values from a column.

14
New cards

ORDER BY

Sorts query results in ascending or descending order.

15
New cards

GROUP BY

Aggregates data based on one column.

16
New cards

HAVING

Filters grouped results (*works with GROUP BY command)

17
New cards

LIMIT

Restricts the number of rows that are returned.

18
New cards

CASE

Adds conditional logic to an SQL query.

19
New cards

UNION

Combines results from multiple SELECT queries.

20
New cards

EXISTS

checks if a subquery returns any rows

21
New cards

BETWEEN

Filters results w/in a range

22
New cards

GRANT

Gives a privilege to a user.

23
New cards

REVOKE

Takes back privileges granted to a user.

24
New cards

COMMIT

Saves all changes made during current transaction

25
New cards

ROLLBACK

Reverts (undos) changes made during current transaction

26
New cards

SAVEPOINT

Sets a point w/in a transaction to which changes can be rolled back (allowing partial rollbacks)

27
New cards

DELETE v.s. TRUNCATE TABLE v.s. DROP TABLE

DELETE

  • Deletes specific rows from table

  • Keeps table structure

  • Rollback is always possible

Use When: You want to remove certain rows w/o affecting table itself.

DELETE FROM employees WHERE department = 'HR'; 

-- This only deletes rows where department = 'HR' 

TRUNCATE TABLE

  • Deletes all records form a table

  • Keeps table structure

  • Rollback is not always possible

Use When: You want to remove all rows from a table quickly

TRUNCATE TABLE employees; 

-- Deletes all data from the employees table, but structure remains

DROP TABLE

  • Deletes table AND deletes its structure, permanently

  • No rollback (irreversible)

Use When: You no longer need a table at all

DROP TABLE employees; 

-- This permanently deletes the employees table, and cannot be undone

28
New cards

What is a ROLLBACK command?

  • A command used to undo changes that have been made to a database w/in a transaction, but have not yet been committed

  • Essentially — it restores database to previous state before changes were executed

29
New cards

In a database, what do each of the following represent:

(a) Rows

(b) Columns

(c) Cells

(a) Rows = Records

(b) Columns = Fields/Attributes (define type of data stored in each record)

(c) Cells = Individual data points

30
New cards

Is SQL generally case-sensitive or case-insensitive?

Describe how case rules apply to keywords/commands, table/column names, aliases/variables, and string values.

SQL is generally case-insensitive, but it is encouraged to follow certain rules when it comes to case:

Keywords & Commands — written in ALL CAPS

  • E.g. SELECT, WHERE, FROM

Tables & Column Names — written in camelCase or Pascal_Case

  • E.g: employees, orderDetails, customer_name

Aliases & Variables — written in camelCase or Pascal_Case

String Values — case doesn’t matter

31
New cards

T or F: Case doesn’t matter for string value comparisons.

True

32
New cards

Data Types

  • Numeric Types

  • Character & String Types

  • Date & Time Types

  • Binary Types

  • Boolean Type

Numeric Types

  • INT — Integer (whole number)

  • DECIMAL (p,s) — Fixed precision numbers

  • FLOAT — Approximate floating-point numbers

Character & String Types

  • CHAR(n) — Fixed-length string

  • VARCHAR(n) — Variable-length string

  • TEXT — Large text storage

Date & Time Types

  • DATE — Stores date only

  • TIME — Stores time only

  • DATETIME — Stores both date & time

Binary Types

  • BINARY(n) — Fixed-length binary data

  • VARBINARY(n) — Variable-length binary data

  • IMAGE — Large binary storage (deprecated)

Boolean Type

  • BIT — Stores 0 (false) or 1 (true)

33
New cards

What is a transaction?

  • Definition

  • Significance

Definition

Group of SQL statements that are executed as a single unit of work.

Transaction

Transactions ensure data integrity & consistency by allowing for the rollback of changes if any part of the transaction fails.

34
New cards

SQL Code — To display all data (records) in a table.

SELECT * 
  FROM tableName; 

-- this displays all fields in table called "tableName"

‘ * ‘ means all fields

35
New cards

SQL Code — To display specific records in a table.

SELECT * 
  FROM tableName; 
  WHERE attributeName = 'someValue'; 

-- this displays records from table called "tableName" where attributeName = someValue

36
New cards

SQL Code

To get around issue of mixed case in a string

SELECT * 
  FROM 
WHERE UPPER(attributeName) = 'BLAH'

-- The data in attributeName is converted to upper case and compare to "BLAH"

37
New cards

SQL Code

To display specific records in a table where a certain field is null.

SELECT * 
  FROM tableName
  WHERE tableAttribute IS NULL; 

38
New cards
SELECT *
  FROM customer
 WHERE custcity = null;

What is wrong with the code?

Line 3 has incorrect syntax

To display the record(s) in the custcity field where the value is null…

 WHERE custcity IS null; -- You have to use keyword 'is'

39
New cards

SQL Code

To specify multiple conditions…

use AND or OR keywords

E.g.

SELECT *
  FROM customer
 WHERE custcity='BRAMPTON'
   AND srepno IS NOT NULL;

40
New cards
--Sample Code 1
SELECT * FROM customer;
SELECT * FROM salesorder;
--Sample Code 2
SELECT * FROM salesorder, customer;

What is the different b/w the sample codes?

The key difference between Example 1 and Example 2 is how they retrieve data from multiple tables.

--Sample Code 1
SELECT * FROM customer;
SELECT * FROM salesorder;
  • Each statement runs independently

  • Returns all records from customer table first, then all records from salesorder table second

  • Use this method for retrieving data instead of sample code 2

--Sample Code 2
SELECT * FROM salesorder, customer;
  • This query performs a cross join

  • It combines every row from salesorder table w/ every row from the customer table

  • If salesorder has 10 rows & customer table hs 5 rows, the result (output) will be 10 × 5 = 50 rows

41
New cards
42
New cards
43
New cards
44
New cards

In SQL, string values are enclosed in __________________.

In SQL, string values are enclosed in single quotes.

45
New cards

How do you include a quote inside of a string?

Use double quote marks

46
New cards

CAST() / CONVERT()

Converts value from one data type to another.

Example:

SELECT CAST(123.45 AS INT);  -- returns 123

*CONVERT() allows re-formatting of date/time

Example:

SELECT CONVERT(VARCHAR, GETDATE(), 101) AS FormattedDate;
  • GETDATE() returns the current date and time.

  • CONVERT() changes it to a VARCHAR string.

  • The style code 101 formats it as mm/dd/yyyy—for example, 06/23/2025.

47
New cards

Demonstrate:

How to add a taxAmount column to a preexisting table called finances.

ALTER TABLE finances
ADD COLUMN taxAmount 

48
New cards

Aggregates

Aggregates refer to functions that perform calculations on a set of values and return a single result.

49
New cards

DDL

  • Stands for…

  • Purpose?

DDL = Data Definition Language

Purpose: Defines structure/schema of database tables.

50
New cards

DML

  • Stands for…

  • Purpose?

DML = Data Manipulation Language

Purpose = Manages data within existing tables.

51
New cards

DCL

  • Stands for…

  • Purpose?

DCL = Data Command Language

Purpose:

52
New cards

Which 4 core commands belongs to DDL?

(HINT: Cool Architects Design Towers)

CREATE

ALTER

DROP

TRUNCATE

53
New cards

Which 4 core commands belongs to DML?

(HINT: Insane Urologists Demand Salaries)

INSERT INTO

UPDATE

DELETE FROM

SELECT

54
New cards

T or F: Strings in SQL can be enclosed within either single quotes or double quotes.

False. Strings in SQL are always enclosed in single quotes.

In SQL Server (T-SQL), double quotes are used only when quoting identifiers, such as column or table names if they contain spaces or reserved keywords — and even then, using [square brackets] is more common:

sql

Copy code

SELECT "first_name" FROM Customers; -- Not common SELECT [first name] FROM Customers; -- Better practice

55
New cards