Podcast
Questions and Answers
What are the four broad categories of SQL?
What are the four broad categories of SQL?
Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Transaction Control Language (TCL)
Which of these is a function used to get the average of a column?
Which of these is a function used to get the average of a column?
The SQL Server data type ______ is used to store whole numbers with no decimal places.
The SQL Server data type ______ is used to store whole numbers with no decimal places.
INT
VARCHAR and NVARCHAR data types are used to store variable-length text.
VARCHAR and NVARCHAR data types are used to store variable-length text.
Signup and view all the answers
What does the SQL Server data type DATETIME store?
What does the SQL Server data type DATETIME store?
Signup and view all the answers
What are the two main components that define a DECIMAL data type in SQL Server?
What are the two main components that define a DECIMAL data type in SQL Server?
Signup and view all the answers
The BIT data type in SQL Server is used to store Boolean values, which can be either 1 or 0.
The BIT data type in SQL Server is used to store Boolean values, which can be either 1 or 0.
Signup and view all the answers
Which of these SQL commands is used to insert new rows into a table?
Which of these SQL commands is used to insert new rows into a table?
Signup and view all the answers
What is the main purpose of the WHERE clause in an SQL statement?
What is the main purpose of the WHERE clause in an SQL statement?
Signup and view all the answers
Explain the difference between the AND and OR operators in SQL.
Explain the difference between the AND and OR operators in SQL.
Signup and view all the answers
What is the purpose of the BETWEEN operator in SQL?
What is the purpose of the BETWEEN operator in SQL?
Signup and view all the answers
What does the LIKE operator achieve in SQL queries?
What does the LIKE operator achieve in SQL queries?
Signup and view all the answers
Explain the purpose of the IN operator in SQL.
Explain the purpose of the IN operator in SQL.
Signup and view all the answers
Match the following SQL operators with their appropriate description.
Match the following SQL operators with their appropriate description.
Signup and view all the answers
What is the difference between DELETE and TRUNCATE commands in SQL?
What is the difference between DELETE and TRUNCATE commands in SQL?
Signup and view all the answers
The SQL JOIN clause helps combine data from multiple tables based on common columns or a defined join condition.
The SQL JOIN clause helps combine data from multiple tables based on common columns or a defined join condition.
Signup and view all the answers
Which of these SQL join types returns all rows from the left table, along with matching rows from the right table, populating missing values on the right side with NULL?
Which of these SQL join types returns all rows from the left table, along with matching rows from the right table, populating missing values on the right side with NULL?
Signup and view all the answers
The UNION operator combines result sets from multiple SELECT queries, removing duplicate rows.
The UNION operator combines result sets from multiple SELECT queries, removing duplicate rows.
Signup and view all the answers
What is the difference between UNION and UNION ALL?
What is the difference between UNION and UNION ALL?
Signup and view all the answers
What is the purpose of the SQL INTERSECT operator?
What is the purpose of the SQL INTERSECT operator?
Signup and view all the answers
How does the SQL CROSS JOIN differ from other join types?
How does the SQL CROSS JOIN differ from other join types?
Signup and view all the answers
A subquery can be used within a WHERE clause to filter data based on results from another query.
A subquery can be used within a WHERE clause to filter data based on results from another query.
Signup and view all the answers
What is the purpose of a correlated subquery in SQL?
What is the purpose of a correlated subquery in SQL?
Signup and view all the answers
The SQL EXISTS operator can be used to check if a subquery returns any rows, impacting whether the main query executes or not.
The SQL EXISTS operator can be used to check if a subquery returns any rows, impacting whether the main query executes or not.
Signup and view all the answers
What is the primary function of the SQL GROUP BY clause?
What is the primary function of the SQL GROUP BY clause?
Signup and view all the answers
The SQL HAVING clause can be used to filter the result set after grouping rows with GROUP BY, applying conditions to the aggregated data.
The SQL HAVING clause can be used to filter the result set after grouping rows with GROUP BY, applying conditions to the aggregated data.
Signup and view all the answers
What is the difference between using a WHERE clause before a GROUP BY and a HAVING clause after a GROUP BY?
What is the difference between using a WHERE clause before a GROUP BY and a HAVING clause after a GROUP BY?
Signup and view all the answers
Aggregate functions in SQL can be used to summarize data within groups, such as finding minimum, maximum, average, sums, or the count of values.
Aggregate functions in SQL can be used to summarize data within groups, such as finding minimum, maximum, average, sums, or the count of values.
Signup and view all the answers
A subquery can only be used within a DELETE command, and it's not possible to use subqueries within other SQL commands like SELECT, UPDATE, or INSERT.
A subquery can only be used within a DELETE command, and it's not possible to use subqueries within other SQL commands like SELECT, UPDATE, or INSERT.
Signup and view all the answers
SQL HAVING is typically used with SELECT and a GROUP BY clause, and it's less common to find it without a GROUP BY clause.
SQL HAVING is typically used with SELECT and a GROUP BY clause, and it's less common to find it without a GROUP BY clause.
Signup and view all the answers
What is a virtual table in the context of subqueries?
What is a virtual table in the context of subqueries?
Signup and view all the answers
SQL Subqueries are used when you want to compare a single value against a list of values, such as checking if a product has a specific code.
SQL Subqueries are used when you want to compare a single value against a list of values, such as checking if a product has a specific code.
Signup and view all the answers
Correlated subqueries execute once for each row in the outer query, making them less efficient than regular subqueries.
Correlated subqueries execute once for each row in the outer query, making them less efficient than regular subqueries.
Signup and view all the answers
Study Notes
SQL (Structured Query Language)
- SQL is a data manipulation language (DML)
- Includes commands for inserting, updating, deleting, and retrieving data within database tables.
- Also a data definition language (DDL) that creates database objects like tables, indexes, and views.
- Commands define access rights to database objects
SQL Data Types
- Define characteristics of data stored in database columns.
- Define possible values accepted.
- INT (Integer): Accepts whole numbers only.
- VARCHAR/NVARCHAR: Variable-length character strings.
- DATETIME: Stores dates and times.
- DECIMAL: Precise decimal values (numeric values with decimal point).
- FLOAT: Long numeric values that are approximations.
- BIT: Boolean values (Yes/No, True/False).
Reasons to use SQL Server Data Types
- Stores data consistently and in a known format.
- Data type allows for specific calculations and formulations.
- Data Types affect storage size (some values take up more space than others.)
- Affects query performance.
Commonly used SQL Server Data Types
- INT: Stores whole numbers (-2,147,483,648 to 2,147,483,647)
- VARCHAR/NVARCHAR: Stores variable length text (with maximum capacity of 8,000 characters for VARCHAR, 4,000 characters for NVARCHAR).
- DATETIME: Stores dates and times from January 1, 1753 to December 31, 9999.
- DECIMAL: Precise decimal values (precision and scale define number of total digits and digits after decimal point).
- FLOAT: Stores approximate numerical values (huge range of numerical values.).
- BIT: Stores boolean (True/False or 1/0) values.
Data Manipulation Commands
- INSERT: Used for inserting new rows into a table. Includes values for each column.
-
UPDATE: Modifies data in a table, specifying the column and new values. Uses the
WHERE
clause to target specific rows, optionally usingAND
orOR
to filter based on multiple conditions, in case more rows need updating. -
DELETE: Removes rows from a table. Uses the
WHERE
clause to target specific rows for deletion, optionally usingAND
orOR
to filter based on multiple conditions. - SELECT: Retrieves data from one or more tables.
- COMMIT: Permanently saves data changes.
-
ROLLBACK: Restores data to their original values before the last
COMMIT
.
Special Operators
- BETWEEN: Checks if a value falls within a specified range of values.
- IS NULL: Checks if a value is null.
- LIKE: Checks if a value matches a given pattern (using wildcards such as % for any sequence of characters, and _ for a single character).
- IN: Checks if a value is present in a given list of values.
- EXISTS: Checks if a subquery returns any rows.
SQL Operators (AND, OR)
-
AND
: Returns TRUE if both conditions are TRUE. -
OR
: Returns TRUE if either condition is TRUE (or both).
Data Types (considerations)
-
CHAR
,VARCHAR
andVARBINARY
data types that are padded with spaces in theSET ANSI_PADDING
setting. -
TRUNCATE TABLE
deletes rows from a table. -
DELETE
statement deletes rows from a table based on a condition. -
DELETE
without aWHERE
clause deletes all rows from the table.
Additional Considerations
- Data Type Conversion: Database errors occur if the values have the wrong data type.
-
Error Handling:
TRY...CATCH
statement can handle errors duringINSERT
,UPDATE
, andDELETE
operations. -
UNIQUE
: constraint ensures unique values in each row. -
PRIMARY KEY
: Marks a specific field. -
DEFAULT
values,CHECK
constraint andNOT NULL
constraint. -
HAVING
clause: Limits or sets conditions for theGROUP BY
clause.
Useful Functions
-
COUNT
: Calculates the number of rows. -
SUM
: Calculates the sum of values within a column. -
AVG
: Calculates the average of values in a column.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.