SQL Theory Flashcards
What’s a rdms?
It stands for relational database management system. It’s a system that manages a relational database. A relational database is a database that uses the relational model, which stores information in the form of tables with rows and columns.
What’s sql?
It stands for structured query language and is the language used in a relational database.
What are the major enterprise relational database systems?
- ms sql
- my sql
- oracle database
- ibm db2
They all their own version of sql. For example:
Oracle has created the PL/SQL extension of SQL, while Microsoft SQL Server uses the Transact-SQL language.
They’re fairly similar to each other but have their own quirks and features.
What is the difference between mySQL and NoSQL databases?
Nosql is not relational. It doesn’t use tables to store data but reather in json with key-value pairs. It also doesn’t have rigid schema like sql, it’s dynamic. Properties in nosql can be added on the fly.
What’s the difference between postgreSQL and mySQL?
They have alot of the same features. Except that PostgreSQL is an object-relational database management system. On the other hand, MySQL uses tables as a core component .
What are the 5 major types of SQL statements?
Data Definition Language (DDL): Used to define or change the database’s structure (e.g. the CREATE, ALTER, and DROP statements).
Data Manipulation Language (DML): Used to change the records present in a database (e.g. the INSERT, UPDATE, and DELETE statements).
Data Query Language (DQL): Used to query the database for information that matches the parameters of the request (e.g. the SELECT statement).
Data Control Language (DCL): Used to set privileges, roles, and permissions for different users of the database (e.g. the GRANT and REVOKE statements).
Transaction Control Language (TCL): Used to save or revert the changes made by DML statements (e.g. the COMMIT and ROLLBACK statements).
What is the difference between a primary key, a unique key, and a foreign key?
The difference between primary and unique key is that a primary key is the column that uniquely identifies a table like product_id or customer_id. While a unique key is a key that can only have unique values, meaning that each record for that column can’t be the same. A foreign key is a key that’s the primary key of another table.
More formal definitions:
1) A primary key is an attribute or attributes serving as a unique identifier for each record.
2) A unique key is an attribute that must be different for each record. Unlike primary keys, there can be multiple unique keys in a table.
3) A foreign key is an attribute in one table that refers to a primary key in a different table.
What are the different types of relationships in a relational database?
- one-to-one: A relationship in which a single record of type A may be linked only to a single record of type B. For example, a student name can only be associated with one student id, and vice versa.
- One to many: A relationship in which a single record of type A may be linked to multiple records of type B. For example, a prof may teach many classes.
- Many to one: A relationship in which multiple records of type A may be linked to a single record of type B. Multiple courses may be taught by the same professor. This is the inverse of a one-to-many relationship.
- Many to many: A relationship in which multiple records of type A may be linked to multiple records of type B. For example, a student may take multiple courses, and each course may be taken by multiple students.
What are the different types of clauses in SQL?
SQL clauses are used to qualify a database query by restricting or altering the values that it returns.
There’s FROM, WHERE, ORDER BY, AS, DISTINCT
GROUP BY: Used to group together rows that have the same values. For example, you may wish to group together students based on their major.
HAVING: Used in combination with the GROUP BY clause. It restricts the returned values to only those that fulfill a given condition.
What are the different types of JOIN clauses in SQL?
A JOIN clause combines records from multiple tables into a single table, based on the common values that they share between one or more columns.
INNER JOIN: returns all records(rows) that have at one match in each table.
LEFT JOIN: Returns all records from the left table and all matching records from the right table.
LEFT JOIN: Returns all records from the right table, and all matching records from the left table.
FULL JOIN: Returns all records that have at least one match in either table.
CROSS JOIN: Returns all possible combinations of rows from the left table and the right table. This is known as the Cartesian product.
What is the difference between the DELETE, TRUNCATE, and DROP commands in SQL?
DELETE: Removes one or more record from a table depending on a condition from a where clause.
TRUNCATE: This removes all the records from a table. It’s not reversible.
DROP: Deletes an entire table
What does a NULL value represent in SQL?
It’s a special signifier that represents no value. You can test whether a field has a NULL value by using the SQL operators IS NULL and IS NOT NULL.
Write a SQL query to retrieve the records of students whose first name begins with the letter D.
select * from students where first_name like ‘D%’
Write a SQL query to remove all records of students who are 21 years old.
delete from students where age = 21
Write a SQL query to retrieve the first 10 records of students with ID numbers that end in 0.
select * from students where student_id like ‘%0’ limit 10