SQL Questions Flashcards
How would you rate yourself in SQL? (from 1 to 10)
- 1-4: Beginner
- 5-6: Intermediate
- 6+: Advanced
Answer 1: From the SDET point of view I would consider my SQL skills to be at an 8 out of 10.
Answer 2: I would rate myself 6 out 10 since I am not a database administrator but can perform the necessary queries as an SDET.
What is a Database?
A database is a structured collection of data that is organized and stored in a way that allows for efficient retrieval, management, and manipulation of that data.
Databases are used to store and manage large volumes of information, making it easy to access, update, and analyze the data.
They are a fundamental component of information systems and are used in a wide range of applications, from simple personal record-keeping to large-scale enterprise systems.
What is RDBMS?
How is it different from DBMS?
RDBMS ⇒ Relational Database Management System.
Relational Databases are structured with tables that are connected through Primary and Foreign key relationships.
Employees
employee_id | name| SSN| DOB| email| phone number| department_id
1 | Muneer | XXXX |values ……
2 | Amro ……….
Departments
department_id| department_name | Department_start_date | department_boss
If the true intention of the employees table is to store data related to employees then only attributes related to employees should be there.
What does SQL stand for?
SQL ⇒ Structured Query Language used in programming and designed for managing and manipulating relational databases.
SQL is commonly used for tasks such as querying data from databases, inserting, updating, and deleting records, creating and modifying database schemas (tables, indexes, views, etc.), and managing permissions and security within a database system.
It provides a standardized way of interacting with databases across different platforms and is widely used in the field of database management and data analysis.
What does Relational mean when we are referring to Relational Database?
SHORT ANSWER: Relational Databases are structured with tables connected through Primary and foreign key relationships.
EXTENDED ANSWER: In the context of a Relational Database, the term “Relational” refers to the way data is organized within the database and the principles upon which the database management system operates.
A Relational Database organizes data into tables, where each table consists of rows and columns.
The relationships between tables are established through common fields, typically referred to as keys, which are used to connect data between different tables.
Are you familiar with any Constraints in SQL?
Constraint ⇒ Rules for relational databases.
Primary Key ⇒ A unique identifier in a table that helps identify a specific row for a given table. Uniquely identifies a record in the table.
Foreign Key ⇒ A foreign is a column within a table that is a primary key in another table.
Unique/composite Key ⇒ A column in a table that is not the primary key but only has unique values.
NOT NULL ⇒ There should always be a value defined.
What TOOLS are you using to work with your database?
- DB Viewer ⇒ MySQL Workbench
- DB server ⇒ MySQL
Currently in my company the application database is a MySQL based server.
I use MySQL Workbench to connect to my application database and run queries.
What is a Primary Key?
Primary Key ⇒ A unique identifier in a table that helps identify a specific row for a given table.
What is a Foreign Key?
Foreign Key ⇒ A foreign is a column within a table that is a primary key in another table.
What is a Unique key?
Unique/composite Key ⇒ A column in a table that is not the primary key but only has unique values.
What is a JOIN?
What are the different types of JOIN?
What is a JOIN? ⇒ The process of combining data between 2 or more tables.
Types of JOIN:
* INNER
* LEFT
* RIGHT
* FULL
* SELF
Have you heard of an INDEX before?
SHORT: Indexes are special items added to database tables to allow faster retrieval of data.
LONG ANSWER: Yes, I’m familiar with indexes in SQL. In a relational database management system (RDBMS), an index is a data structure that improves the speed of data retrieval operations on a database table at the cost of additional space and slower inserts, updates, and deletes. Indexes are created on one or more columns of a table and are used to quickly locate rows that match a given search condition, typically by reducing the number of rows that need to be examined during query execution.
Types of Indexes:
- Single-Column Index: An index created on a single column of a table.
- Composite Index: An index created on multiple columns of a table, allowing for more complex query optimizations.
- Unique Index: An index that enforces uniqueness on the indexed column(s), preventing duplicate values.
What is a Subquery in SQL?
SHORT: A query within a query.
LOND: A subquery, also known as an inner query or nested query, is a query nested within another SQL query. Subqueries are enclosed within parentheses and are used to retrieve data that will be used as a condition or value in the outer query.
Subqueries can be used in various parts of an SQL statement, such as the SELECT, FROM, WHERE, HAVING, and JOIN clauses. They provide a powerful way to perform complex data manipulations and filtering operations by allowing you to break down a problem into smaller, more manageable parts.
Example:
When you need another query to filter a specific value that becomes an input to your main query.
SELECT *
FROM table
WHERE column = (Inner query);
Need to find the employees that make more than the average salary?
SELECT employee_name, phone, email
FROM employees
WHERE avg salary > (SELECT avg salary from employees);
What is UNION? What is the difference between UNION and UNION ALL?
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
ANSWER 1:
UNION ⇒ When you combine the output between multiple independent select statements. (removes duplicate)
UNION ALL => (output will be duplicates and non duplicates)
Rules:
1. Same number of columns in each query.
2. Order of the column and data type needs to match.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
ANSWER 2: The result of a UNION operation includes all distinct rows from both SELECT queries, and duplicate rows are eliminated.
On the other hand, UNION ALL also combines the results of two or more SELECT queries into a single result set, but it does not remove duplicate rows. It includes all rows from all SELECT queries, regardless of whether they are duplicates.
In summary, UNION combines the results of multiple SELECT queries into a single result set, removing duplicate rows, while UNION ALL combines the results without removing duplicates. The choice between UNION and UNION ALL depends on the specific requirements of the query and whether duplicate rows need to be retained or eliminated.
Have you heard of a STORED procedure before?
A STORED procedure is a custom database function that is created for a specific process. Developers or Database Engineers create STORED procedures.
Can you JOIN more than 2 tables?
When you are joining, what are you looking for within the tables to ensure you can join them?
- YES!
JOIN criteria:
- Primary Key and foreign key relationship.
- Common column.
- Common data type.
What are the Usages of SQL?
- Creating new databases
- Creating new tables in a database
- Inserting records in a database
- Updating records in a database
- Deleting records from a database
- Retrieving data from a database
- Executing queries against a database
- Creating stored procedures in a database
- Creating views in a database
- Setting permissions on tables, procedures, and views
- …and many other data storage operations
What are important SQL Language Elements?
- Identifiers: Names of Database objects such as tables, views, columns, and databases etc…
- Data Types: Define the type of data that is contained by a column.
- Constants: Symbols that represent specific data types.
- Operators: Perform Arithmetic, Comparison, and Logical Operations.
- Functions: Built-in Functions to perform specific operations.
- Clauses: Constituent components of statements and queries.
- Expressions: Produce either scalar values, or tables consisting of columns and rows of data.
- Queries: Retrieve the data based on specific criteria. This is an important element of SQL.
What are the Sub Sets of SQL?
- DDL - Data Definition Language
- DML - Data Manipulation Language
What is Data Definition Language?
Data Definition Language (DDL) - allows us to create, alter, and delete database objects such as schemas, tables, views, sequences, catalogs, indexes and aliases.
What is Data Manipulation Language?
Data Manipulation Language (DML) is a language which enables users to access and manipulate data.
DML is used to Perform below operations:
* Insertion of data into the database
* Retrieval of data from the database
* Updating data in the database
* Deletion of data in the database
What is Database?
A Database (DB) is a systematic collection of data, Databases support storage and manipulation of data. Using a databases makes data management easy.
What is Table?
A Table in a Relational Database is a predefined format of rows and columns that define an entity.
Each column contains a different type of attribute and each row corresponds to
a single record.
Each table is provided with a name.
What is DBMS?
A DataBase Management System (DBMS) is software designed to assist in maintaining and utilizing large collection of data.
The alternative to using a DBMS is to store the data in files and write application-specific code to manage it.
Using a DBMS to manage data has many advantages like:
- Data independence
- Efficient data access
- Data integrity and security
- Data administration
- Concurrent access and data recovery
What is MySQL?
MySQL is open source Database Management System, developed by Swedish company MySQL AB.
MySQL supports many different platforms including Microsoft Windows, the major Linux distributions, UNIX, and Mac OS X.
MySQL has free and paid versions, depending on its usage (non- commercial/commercial) and features.
MySQL comes with a very fast, multi-threaded, multi-user, and robust SQL database server.
What are the categories of Operators available in SQL?
- Arithmetic operators
- Comparison operators
- Logical operators
What are Arithmetic operators in SQL?
Arithmetic Operator in SQL:
- + (Addition) - Adds values
- - (Subtraction) - Subtracts Right side value from Left side value
- * (Multiplication) - Multiplies values on either side of the operator
- / (Division) - Divides left hand operand by right hand operand
- % (Modulus) - Divides left hand operand by right hand operand and returns remainder