SQL Flashcards

1
Q

What is a Primary key?

A

Primary Key (PK) is a column in the table, it’s uniquely identifies each record, and value is never duplicated within the same table. Primary Key cannot contain NULL values, a primary key is a special column in a database table that uniquely identifies each record. It must have a unique value for each row and cannot have null values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a Foreign key?

A

Yes, Foreign key is usually found in another table. A key is used to connect or link two tables in a database. It can allow for the acceptance of Null Values. Yes, it’s possible to have more than one Foreign Key in a table, a foreign key is like a special field in one table that points to the primary key or a unique key in another table. It helps link information between tables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How would you avoid duplicate records in a SELECT query?

A

Using the DISTINCT identifier eliminates duplicate records.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When and why do we use ORDER BY Clause?

A

in a SQL query it is used to sort the result-set in either ascending or descending order. By default, it sorts records in ascending order. If we want to sort them in descending order, we can use the DESC keyword after the ORDER BY.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the difference between BETWEEN and IN operators?

A

They are used for filtering based on multiple values, but they have distinct uses: The BETWEEN operator is used to select a range of data between two values. The IN operator allows us to specify multiple specific values to be included in the result set.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the difference between AND and OR operators?

A

The AND and OR operators in a WHERE clause they are used to filter records based on multiple conditions: The AND operator displays a record if all the conditions separated by AND are TRUE. The OR operator displays a record if any of the conditions separated by OR is TRUE.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Let’s say there are some null values in my table. How would you test those values?

A

A field with a NULL value means it has no value. Comparing NULL values using traditional comparison operators like ‘=’, ‘<’, or ‘<>’ doesn’t work because NULL cannot be directly compared with other NULL values. To test for NULL values, you need to use the IS NULL operator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When and why do we use GROUP BY Clause?

A

The GROUP BY clause in SQL is utilized to group the results of a SELECT query based on one or more columns. This statement is commonly paired with aggregate functions such as COUNT, MAX, MIN, SUM, and AVG to summarize data within each group.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Are you familiar with functions in SQL? What type?

A

Yes, I’m familiar with SQL functions. Such as LOWER, UPPER, and MAX, MIN, AVG. ( LOWER(): Changes a text to lowercase. UPPER(): Converts text to uppercase. MAX(): Gives the highest value in a set of data.MIN(): Provides the lowest value in a set. AVG(): Calculates the average value from a set of numbers)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the difference between the WHERE and HAVING clauses?

A

WHERE clause is used for filtering rows and it applies on each and every row, while HAVING clause is used to filter groups in SQL. One syntax level difference is, WHERE is used before GROUP BY clause, while HAVING is used after GROUP BY clause.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a Subquery?

A

A subquery is a query inside another query, also called a nested query. It gets data used as a condition in the main query, helping to limit the information retrieved.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the difference between an inner and outer join?

A

An inner join shows rows when there is matching data in two or more compared tables. An outer join displays rows from both tables, including unmatched records from one or both tables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How would you retrieve data that is located in 2 different tables?

A

Using the join of two tables based on condition we can retrieve data from two tables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is Self Join?

A

self JOIN is like regular join, but table is connected with itself.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How would you find the second highest salary from the table? Third highest?

A

To find the second highest salary from the table, SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees); This query selects the maximum salary from the “employees” table where the salary is less than the maximum salary in the entire table, effectively giving us the second highest salary. For the third highest salary, SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees)); This query builds on the logic of the second highest salary query, ensuring that the selected salary is less than both the maximum and the second maximum salaries in the table.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the difference between UNION and UNION ALL?

A

UNION combines data from two tables with similar structures into one table. It removes duplicate records and sorts the results. But, UNION ALL includes duplicates. UNION’s performance is usually slower than UNION ALL because it needs to eliminate duplicates.

16
Q

What is the difference between DML & DDL Commands? DCL?

A

Data Definition Language (DDL) is about defining and managing database schemas, outlining how data should be structured in the database. (drop, rename, create, alter, truncate) Data Manipulation Language (DML) focuses on manipulating data, including operations to retrieve, store, modify, delete, and update data in the database. (select, update, delete, insert) Data Control Language (DCL) handles rights, permissions, and other controls within the database system. (revoke, grant) Transaction Control Language (TCL) manages transactions within the database. (commit, rollback)

17
Q

What is the difference between DELETE and TRUNCATE?

A

DELETE: Removes specific rows from a table using a WHERE clause for conditions. Deleted records can be rolled back or committed. TRUNCATE: Deletes all rows from a table and automatically commits the transaction. Unlike DELETE, it cannot be rolled back.

18
Q

How do you perform Database Testing? How do you connect to Database in Automation?

A

In my project, I work with a MySQL relational database stored on AWS. For manual tasks, I use MySQL Workbench to execute SQL queries directly on our database. For automation, I employ JDBC (Java DataBase Connectivity API), a Java-based library facilitating interactions with various databases. Firstly, I create a connection to our database using the DriverManager class, passing parameters such as DatabaseURL, username, and password. The Database URL follows the format “jdbc: type of jdbc: host: port: database name.” Once the connection is created, I prepare a Statement Object to send SQL queries to the database. The results are then stored in a ResultSet Object.

18
Q

What is the difference between Commit and Rollback?

A

Both ROLLBACK and COMMIT are Transaction Control Commands (TCC) with distinct purposes: COMMIT is employed to permanently save changes made to the server, making them a permanent part of the database. ROLLBACK, is used to undo changes, reverting to the previous state, discarding any modifications made during the current transaction.

19
Q

What type of driver do you use to connect to the Database?

A

We are using MySQL driver that we specify in our pom.xml file in the form of dependencies

20
Q

What is metadata?

A

The metadata means data about data we can get further information from the data. JDBC Metadata supports: ● DatabaseMetaData(will give information about the Database itself, such as name or version) ● ResultSetMetaData

21
Q

How do you process results from the Database?

A

Results from the database are returned in a ResultSet Object, and in the project, I store this data into a List<Map>, one of Java’s data structures. For efficient handling of database operations, I’ve created a Utility class. This class contains reusable functions to open and close connections, process ResultSet data, and store it in the form of a List<Map>. This approach allows for organized and reusable database-related operations in the project.