Advanced_data Flashcards
What are relational databases? Explain the theory behind them!
- Relational databases store structured data in tables with defined relationships between them, following the principles of the relational model.
Why SQL is important these days?
- SQL is essential for managing and querying large datasets in relational databases, which are prevalent in modern applications and it is ensuring data security and integrity.
All the new GUI tools enable me to click a button to write SQL. Why should I spend time learning to write SQL manually?
- Understanding SQL enables greater control, customization, and troubleshooting in complex queries beyond what GUI tools offer.
If SQL is standardized, should you be able to program with SQL on any databases?
- Yes, you can generally write and execute standard SQL queries on different relational databases. However, there might be differences in syntax and supported features, which could require adjustments when working with specific databases. These variations are often related to non-standard features, data types, functions, and performance optimizations that are implemented differently by different DBMS vendors.
What makes SQL a nonprocedural language?
- SQL is considered a nonprocedural language because it focuses on specifying what you want to achieve rather than how to achieve it. In other words, you describe the desired outcome, and the database management system (DBMS) figures out the most efficient way to execute your request.
What can you do with SQL?
- SQL allows you to query, insert, update, and delete data, create and modify database structures, and define user access permissions.
Do the following statements return the same or different output? Why?
SELECT * FROM CHECKS; select * from checks;
- The statements return the same output, as SQL is case-insensitive for keywords but not for column/table names.
The following queries do not work when entered into the command line psql console. Why not?
Select * Select * from checks Select amount name payee FROM checks;
- The first needs to specify the table from which we want to select all columns like line 2. The third line must have a comma between columns name.
What shorthand could you use instead of WHERE a >= 10 AND a <=30
?
- I can use
WHERE a BETWEEN 10 AND 30
Which function capitalizes the first letter of a character string and makes the rest lowercase?
- The INITCAP() function performs this action.
Will this query work? Why?
SELECT COUNT(LASTNAME) FROM CHARACTERS;
- The query will work if there’s a table named “CHARACTERS” with a column “LASTNAME” since COUNT operates on columns.
Assuming that they are separate columns, which function(s) would splice together FIRSTNAME and LASTNAME?
- The CONCAT() function can combine FIRSTNAME and LASTNAME.
What is the function of the GROUP BY clause?
- The
GROUP BY
clause in SQL is used to group rows from a table into summary rows based on the values in one or more columns. It’s often used in combination with aggregate functions likeSUM
,COUNT
,AVG
,MAX
, andMIN
to perform calculations on groups of data rather than on individual rows. This is particularly useful for generating reports, performing analysis, and summarizing data.
SELECT CustomerName, SUM(Amount) AS TotalAmount FROM Orders GROUP BY CustomerName;
Why are so few functions defined in the ANSI standard and so many defined by the individual implementations?
- Database systems often introduce proprietary functions to differentiate themselves, leading to a variety of extensions beyond the standard.
When using the HAVING clause, do you always have to use a GROUP BY also?
- Yes, HAVING is used in conjunction with GROUP BY to filter grouped data.
- Use the WHERE clause for filtering individual rows based on column values, and use the HAVING clause for filtering groups of rows based on aggregate values after using GROUP BY.
Can you use ORDER BY on a column that is not one of the columns in the SELECT statement?
yes, you can:
SELECT ship_address from orders
ORDER BY employee_id;
Explain the different kind of joins! (outer, inner, left, right, natural, etc.)
- Inner join: Returns only the matching rows from both tables.
- Left join: Returns all rows from the left table and matching rows from the right table.
- Right join: Returns all rows from the right table and matching rows from the left table.
- Outer join: Includes unmatched rows from one or both tables.
- Natural join: Automatically matches columns with the same name, excluding duplicates.
How many tables can you join on?
You can join multiple tables in a single query, but the specific limit varies based on the database system.
Would it be fair to say that when tables are joined, they actually become one table?
- No, when tables are joined, they are logically combined for the duration of the query, but they remain separate physical entities.
How many rows would a two-table join produce if one table had 50,000 rows and the other had 100,000?
- The number of rows produced depends on the join condition. An inner join with suitable conditions might produce fewer rows than either table, while an outer join could produce more.
What type of join appears in the following SELECT statement?
SELECT e.name, e.employee_id, ep.salary FROM employee_tbl e, employee_pay_tbl ep WHERE e.employee_id = ep.employee_id;
- This statement uses an inner join based on the equality of the “employee_id” columns.
In joining tables are you limited to one-column joins, or can you join on more than one column?
- You can join on multiple columns, enabling more specific matching conditions.
Does SQL have a statement for file import/export operations?
Yes, SQL provides commands like COPY or LOAD for importing/exporting data to/from files.
Can you copy data from a table into itself using the INSERT command? You would like to make duplicate copies of all the existing records and change the value of one field.
- Yes, you can use the INSERT INTO … SELECT statement to achieve this by selecting data from the same table and modifying the necessary field.
INSERT INTO Employee (EmployeeID, FirstName, LastName, Department) SELECT EmployeeID, FirstName, LastName, 'NewDepartment' AS Department FROM Employee;
What would happen if you issued the following statement?
DELETE * FROM COLLECTION;
- The statement is invalid. It should be
DELETE FROM COLLECTION;
without the asterisk.
Can you remove columns with the ALTER TABLE statement?
- Yes, you can use the ALTER TABLE statement to remove columns, but this action may involve data loss.
ALTER TABLE Employee DROP COLUMN Department;
Is the DROP TABLE
command functionally equivalent to the DELETE FROM table_name
command?
- No,
DROP TABLE
removes the entire table and its structure, whileDELETE FROM
only removes data but leaves the table structure intact.
What is the difference between the functionality of the DELETE FROM table_name
and the TRUNCATE TABLE
command?
-
DELETE FROM
removes individual rows with the option to use aWHERE
clause, whileTRUNCATE TABLE
removes all rows, but it’s faster and can’t be rolled back.
When a table is created, who is the owner?
- The owner of a table is typically the user who executed the
CREATE TABLE
statement, or the user specified in the statement if it includes an OWNER clause.
If data in a character column has varying lengths, what is the best choice for the data type?
- The
VARCHAR
data type is the best choice for character columns with varying lengths.