Automation QA Testing Course Content

SQL Interview Questions And Answers

 

 SQL Interview Questions And Answers

1)Find 2nd Highest Salary in SQL

1)SELECT MAX(SALARY) FROM Employees WHERE SALARY < (SELECT MAX(SALARY) FROM Employees);


2)SELECT MAX(SALARY) FROM Employees WHERE SALARY NOT IN (SELECT MAX(SALARY) FROM Employees);


3)SELECT MIN(SALARY) FROM (SELECT SALARY FROM Employees ORDER BY SALARY DESC LIMIT 2);



2)what is the query to return Name and Salary of employee Having Max Salary?

Ans)select name, salary from (select * from salary_table order by salary desc limit 1)

2)SELECT Name, Salary FROM Minions
WHERE Salary = (SELECT Max(Salary) FROM Minions) LIMIT 1

3)select name, salary
from employee
order by salary desc
limit 1

.3)Write an SQL query to find names of employees start with ‘A’? 
The LIKE operator of SQL is used for this purpose. It is used to fetch filtered data by searching for a particular pattern in the where clause. 
The Syntax for using LIKE is, 
 

SELECT column1,column2 FROM table_name WHERE column_name LIKE pattern; 

LIKE: operator name
pattern: exact value extracted from the pattern to get related data in
result set.

The required query is: 

SELECT * FROM Employees WHERE EmpName like 'A%' ;
4)What is the difference between primary key and unique constraints? 
The primary key cannot have NULL values, the unique constraints can have NULL values. There is only one primary key in a table, but there can be multiple unique constraints. The primary key creates the clustered index automatically but the unique key does not.


5)What is the difference between DELETE and TRUNCATE commands?
The DELETE statement removes rows one at a time and records an entry in the transaction log for each deleted row.TRUNCATE TABLE removes the data by deallocating the data pages used to store the table data and records only the page deallocations in the transaction log.
DELETE command is slower than TRUNCATE command.While the TRUNCATE command is faster than the DELETE command.
To use Delete you need DELETE permission on the table.To use Truncate on a table we need at least ALTER permission on the table.
Identity of column retains the identity after using DELETE Statement on table.Identity of the column is reset to its seed value if the table contains an identity column.
The delete can be used with indexed views.Truncate cannot be used with indexed views
6)What is the difference between TRUNCATE and DROP statements?
S.NO.DROPTRUNCATE
1.The DROP command is used to remove table definition and its contents.Whereas the TRUNCATE command is used to delete all the rows from the table.
2.In the DROP command, table space is freed from memory.While the TRUNCATE command does not free the table space from memory.
3.DROP is a DDL(Data Definition Language) command.Whereas the TRUNCATE is also a DDL(Data Definition Language) command.
4.In the DROP command, a view of the table does not exist.While in this command, a view of the table exists.
5.In the DROP command, integrity constraints will be removed.While in this command, integrity constraints will not be removed.
6.In the DROP command, undo space is not used.While in this command, undo space is used but less than DELETE.
7.The DROP command is quick to perform but gives rise to complications.While this command is faster than DROP.

7) Why do we use  Commit and Rollback command?

COMMITROLLBACK
COMMIT permanently saves the changes made by the current transaction.ROLLBACK undo the changes made by the current transaction.
The transaction can not undo changes after COMMIT execution.Transaction reaches its previous state after ROLLBACK.
When the transaction is successful, COMMIT is applied.When the transaction is aborted, ROLLBACK occurs.
8)How can you fetch common records from two tables?

The below statement could be used to get data from multiple tables, so, we need to use join to get data from multiple tables.

Syntax :

SELECT tablenmae1.colunmname, tablename2.columnnmae    
FROM tablenmae1  
JOIN tablename2  
ON tablenmae1.colunmnam = tablename2.columnnmae
ORDER BY columnname;  
9) How would you write a query to identify employees belonging to a particular department?

Sample answer:

Finding data entries belonging to a particular group (in this case, employees belonging to a particular department) can be achieved in a few ways. These include:

  • Use SELECT, FROM, and WHERE statements
  • Use SELECT, FROM, GROUP BY, and HAVING statements
  • Use SELECT, FROM, INNER JOIN, and WHERE statements
10)How do you delete a column?

Sample answer:

A column in a table can be deleted by following these steps:

  • Use ‘ALTER TABLE table name’ to select the table with the column you want to delete
  • Use ‘DROP COLUMN column name’ to select the column you want to delete

  • 11)What are some of the most important aggregate functions?

    Sample answer:

    Aggregate values are used to perform calculations on a set of values to return a single value. Some of the most widely used aggregate functions are:

    • AVG calculates the average set of values
    • COUNT counts the total number of rows in a table
    • MIN finds the minimum value in a table
    • MAX finds the maximum value in a table
    • SUM calculates the sum of the values
  • 12)How would you write a query to find employees with the same salary from an employee table?

    Sample answer:

    To find employees with the same salary, the following solution can be used:

    • Use the SELECT statement to specify the relevant table
    • Use the FROM statement to specify the employee column
    • Use the WHERE statement to specify the salary criteria, for example: 

    ‘WHERE salary IN

    (SELECT salary

    FROM employee 

    WHERE employee.employee_id <> employee.employee_id)’

  • 13)

    How do you remove duplicate rows from a table?

    Sample answer:

    There are several ways to remove duplicate rows from a table. These include:

    • Using Common Table Expressions (CTE) with the ROW_NUMBER function to identify and remove duplicate rows
    • Using the RANK function with the PARTITION BY clause 
    • Using the GROUP BY clause with the COUNT function, and then replacing SELECT with DELETE FROM

    14. How do you create a trigger with SQL?

    Sample answer:

    A trigger is a type of stored procedure that runs when a specific event occurs, such as when a new record is added to the database. 

    Trigger creation varies depending on the RDBMS. Some systems feature a CREATE TRIGGER statement, while others require the users to navigate to a triggers folder in the toolbar. Once created, users must write the trigger’s code, specifying its conditions and effect.

    15. What are the main differences between HAVING and WHERE SQL clauses?

    Sample answer:

    The key differences between HAVING and WHERE SQL clauses are:

    • The WHERE clause is used in row operations, whereas the HAVING clause is used in column operations
    • The WHERE clause comes before GROUP BY in a query, whereas the HAVING clause comes after GROUP BY
    • The WHERE clause cannot be used with aggregate functions, contrary to the HAVING clause

    16. How can sensitive information be stored securely in a database?

    Sample answer:

    Encrypting databases with sensitive information is crucial for security reasons. Encryption can be implemented in several ways. For example, you could:

    • Create a master key
    • Use symmetric encryption
    • Create a certificate protected by the master key

    17. How would you write a query to find the sixth highest-earning employee from an employee table?

    Sample answer:

    A correlated subquery can be used to find the sixth highest-earning employee in a table. The steps are as follows:

    • Use the SELECT statement to specify the employee name and salary columns
    • Use the FROM statement to specify the relevant table
    • Use the WHERE statement to specify the salary criteria, for example:

    ‘WHERE statement 6-1 = (SELECT COUNT(DISTINCT salary) FROM #Employee e2

     WHERE e2.salary > e1.salary)’

    18. What are some ways to prevent duplicate entries when making a query?

    Sample answer:

    There are several methods to avoid duplicate entries when making a query, such as to:

    • Create a unique index
    • Add the DISTINCT keyword to the SELECT statement
    • Use the NOT EXISTS or NOT IN commands
  • 19)

    How to read TOP 5 records from a table using a SQL query?

  • Ans)SELECT * FROM Departments WHERE ROWNUM <= 5;
20)

How to read LAST 5 records from a table using a SQL query?

Ans)Consider the same DEPARTMENTS table as source discussed in previous question.

In order to select the last 5 records we need to find (count of total number of records – 5) which gives the count of records from first to last but 5 records.

Using the MINUS function we can compare all records from DEPARTMENTS table with records from first to last but 5 from DEPARTMENTS table which give the last 5 records of the table as result.

MINUS operator is used to return all rows in the first SELECT statement that are not present in the second SELECT statement.

Query:

SELECT * FROM Departments

MINUS

SELECT * FROM Departments WHERE ROWNUM <= (SELECT COUNT(*)-5 FROM Departments);

21)

How to select UNIQUE records from a table using a SQL Query?

METHOD-1: Using GROUP BY Function

GROUP BY clause is used with SELECT statement to collect data from multiple records and group the results by one or more columns. The GROUP BY clause returns one row per group. By applying GROUP BY function on all the source columns, unique records can be queried from the table.

Below is the query to fetch the unique records using GROUP BY function.

Query:

SELECT EMPLOYEE_ID, 
       NAME, 
       SALARY 
FROM EMPLOYEE 
GROUP BY EMPLOYEE_ID, NAME, SALARY;

What is the difference between primary key and unique key?

Both primary and unique keys carry unique values but a primary key cannot have a null value, while a unique key can. In a table, there cannot be more than one primary key, but there can be multiple unique keys.




















Selenium 4 -Key Features

 

What’s new in Selenium WebDriver 4?

 A significant change under the hood for WebDriver is the complete W3C compliance of the WebDriver APIs. This standardization will eliminate the need for encoding and decoding the API requests by the JSON wire protocol in Selenium 3 and earlier versions for communication between browsers and test scripts. This means the WebDriver will now interact directly with the target browser.

This standardization will result in more stable cross browser tests. However, the change in protocol won’t impact existing users as popular browser drivers like ChromeDriver and GeckoDriver have completely adopted W3C protocols. In a nutshell, JSON wire protocol is deprecated in Selenium 4.

This standardization will result in more stable cross browser tests. However, the change in protocol won’t impact existing users as popular browser drivers like ChromeDriver and GeckoDriver have completely adopted W3C protocols. In a nutshell, JSON wire protocol is deprecated in Selenium 4.

Keynote from Selenium Documentation: The native support for Opera and Phantom JS has been terminated in Selenium 4 as their WebDriver implementations are no longer active. For optimizations on Opera, QAs can rely on Chrome testing as Opera too is based on Chromium. For PhantomJS, users should use Chrome or Firefox in headless mode.

Difference Between Selenium 3 And Selenium 4

Selenium 3
Selenium 4
In Selenium 3, the chrome driver extends directly to the Remote Web Driver.In Selenium 4 Chrome driver class extends to Chromium Driver, which has some predefined methods to access the dev tool.
In Selenium 3, JSON Wire Protocol was the major mode of communication between the test code and the browser.Effectively, Selenium 4 uses the WebDriver W3C protocol as it has W3C compliance.
A difficult task in Selenium 3 was that testers were always to start the Hub and Node jars each time to perform automation testing on the Grid.In Selenium 4, the testers need not start the hub and node jars every time they perform the automation testing.

Features of Selenium 4

1. Enhanced Selenium Grid

The earlier version of Selenium Grid was complex to set up and rigid in terms of scaling.

The new Selenium Grid comes with Docker support. This will enable developers or testers to spin up the containers rather than setting up heavy virtual machines. Moreover, it is redesigned in a way that will allow QAs to deploy the grid on Kubernetes for better scaling.

Managing Selenium Grid is now smooth and easy as there will no longer be any need to set up and start hubs and nodes separately.

Teams or individual testers can now deploy the grid in three modes:

  1. Standalone mode
  2. Hub and Node
  3. Fully distributed

Unlike in earlier versions, the Grid will now support IPv6 addresses and one can communicate with the Grid using the HTTPS protocol. In Grid 4, the configuration files used for spinning up the grid instances can be written in TOML (Tom’s Obvious, Minimal Language) which will make it easier for humans to understand.

The Grid in Selenium 4 also comes with an enhanced user-friendly GUI. Overall, the revamped Selenium Grid will enhance the DevOps process as it provides compatibility with tools like Azure, AWS, and more.

2. Upgraded Selenium IDE

Most QA engineers using the Selenium tool suite are familiar with the popular record and playback tool – Selenium IDE. This Selenium IDE was earlier available just as a Firefox add-on. Later, it got deprecated with the introduction of the latest Firefox versions. This is because the add-ons in the latest Firefox (ver. 55) were standardized under the Web Extension mechanism.

With Selenium 4, the IDE is revived and now its add-on is available for major web-browsers like Firefox and Chrome. The add-on for Selenium IDE is now also available on the MS store.

The new Selenium 4 IDE provides some notable features like:

  1. Improved GUI for intuitive user experience.
  2. The new IDE also comes bundled with a SIDE tool aka Selenium IDE runner. It allows QAs to run .side projects on a node.js platform. This SIDE runner also enables individual QAs to run cross browser tests on local or Cloud Selenium Grid.
  3. Improved control flow mechanism that enables testers to write better “while” and “if” conditions.
  4. The new IDE comes with an enhanced element locator strategy (Like a backup strategy) which helps locate an element in case the web element couldn’t be located. It will result in the creation of stable test cases.
  5. The code for test cases recorded using Selenium IDE can be exported in the desired language binding like Java, C#, Python, .NET, and JavaScript.

3. Relative Locators in Selenium 4

Selenium 4 brings an easy way of locating elements with the inclusion of relative locators. This means testers can now locate specific web elements using intuitive terms that are often used by users like:

  1. To left of
  2. To right of
  3. Above
  4. Below

The introduction of this new method in Selenium 4 helps locate web elements based on the visual location relative to other DOM elements. One can refer to this source to learn more about the implementation of relative locators in detail.

4. Improved Documentation

The documentation section has been revamped significantly with a neat UI for navigating to the desired section or page. This will help testers and developers find relevant information they need for a specific tool, language binding, etc.

The exhaustive documentation covers information about all tools and APIs under the Selenium umbrella. This will help individual testers (particularly the beginners) get acquainted with all the features and prerequisites to get started with automation testing.

5. Support for Chrome Debugging Protocol

Selenium 4 comes with native support for Chrome DevTools Protocol. This means QAs can now use Chrome development properties like Fetch, Network, Profiler, Performance, Application cache, and more. QAs can also leverage the APIs offered by Chrome DevTools to simulate poor network conditions and perform geolocation testing.

Using this API will also help developers or QAs to test and resolve critical bugs for specific web-pages faster and on the fly.

6. Better Window/Tab Management in Selenium 4

There are several instances in test automation wherein one might need to open a particular link in a new tab or window to perform certain actions. To achieve this in Selenium 3, QAs had to create a new driver object and then perform the switch operation using the WindowHandle method to perform subsequent steps.

This is set to change in Selenium 4 as it comes with a new API – newWindow that allows users to create and switch to a new window/tab without creating a new WebDriver object.

Sample code snippet to open a new window

driver.get("https://www.google.com/");
// Opens a new window and switches to new window
driver.switchTo().newWindow(WindowType.WINDOW);

// Opens BrowserStack homepage in the newly opened window
driver.navigate().to("https://www.linkedin.com/");

Sample code snippet to open a new tab within the same window

driver.get("https://www.google.com/");

// Opens a new tab in existing window
driver.switchTo().newWindow(WindowType.TAB);

// Opens Browserstack homepage in the newly opened tab
driver.navigate().to("https://www.linkedin.com/");

7. Deprecation of Desired Capabilities

Desired Capabilities were primarily used in the test scripts to define the test environment (browser name, version, operating system) for execution on the Selenium Grid.

In Selenium 4, capabilities objects are replaced with Options. This means testers now need to create an Options object, set test requirements, and pass the object to the Driver constructor.

Listed below are the Options objects to be used going forward for defining browser-specific capabilities:

  1. Firefox – FirefoxOptions
  2. Chrome – ChromeOptions
  3. Internet Explorer (IE) – InternetExplorerOptions
  4. Microsoft Edge – EdgeOptions
  5. Safari – SafariOptions

8. Modifications in the Actions Class

Actions class in Selenium is primarily used to simulate input actions from mouse and keyboard on specific web elements (For eg: Left click, Right click, Double click, etc)

In Selenium 4, several new methods have been added to the Actions class:

  • click(WebElement)

This method is added to Actions class to replace the moveToElement(onElement).click(). It is used to click on a certain web element.

  • clickAndHold(WebElement)

This method will replace the moveToElement(onElement).clickAndHold(). It is used to click on an element without releasing the click.

  • contextClick(WebElement)

This method will replace moveToElement(onElement).contextClick(). It will perform the right click operation.

  • doubleClick(WebElement)

This method is added to replace moveToElement(element).doubleClick(). It will perform a double click on an element.

  • release()

This method (user for releasing the pressed mouse button) was initially a part of org.openqa.selenium.interactions.ButtonReleaseAction class. Now with the updated version of Selenium, it has been moved to Actions class.

Considering all the points above, one can conclude that the features in Selenium 4 appear quite promising in terms of making the test process easier. However, one must also bear in mind that, regardless of the Selenium version in use, the tests must be performed on real devices for accurate test results.