Top Questions and Answers of SQL Queries asked in Interviews - REQMAT BLOGSPOT
Top Questions and Answers of SQL Queries asked in Interviews

Top Questions and Answers of SQL Queries asked in Interviews

REQMAT BLOGSPOT - Nareddula Rajeev Reddy NRR

How would you write a SQL query to retrieve all rows from a table named "customers" where the customer's city is 'New York'?


Answer: To retrieve all rows from a table named "customers" where the customer's city is 'New York', you can use the following SQL query:

```sql

SELECT *

FROM customers

WHERE city = 'New York';

```


How would you write a SQL query to retrieve all columns from a table named "orders" where the order ID is greater than 1000?


Answer: To retrieve all columns from a table named "orders" where the order ID is greater than 1000, you can use the following SQL query:

```sql

SELECT *

FROM orders

WHERE order_id > 1000;

```


How would you write a SQL query to retrieve the first name, last name, and email address of all customers who have made a purchase in the last 30 days?


Answer: To retrieve the first name, last name, and email address of all customers who have made a purchase in the last 30 days, you can use the following SQL query:

```sql

SELECT customers.first_name, customers.last_name, orders.email_address

FROM customers

JOIN orders ON customers.customer_id = orders.customer_id

WHERE orders.order_date >= DATEADD(day, -30, GETDATE()) ;

```


How would you write a SQL query to calculate the average price of all products sold in the month of June?


Answer: To calculate the average price of all products sold in the month of June, you can use the following SQL query:

```sql

SELECT AVG(product_price) AS avg_price

FROM sales_data

WHERE MONTH(sales_date) = 6;

```


How would you write a SQL query to update the quantity of a product with ID 123 to 25?


Answer: To update the quantity of a product with ID 123 to 25, you can use the following SQL query:

```sql

UPDATE products SET quantity = 25 WHERE product_id = 123;

```


What is the difference between WHERE and HAVING clauses in SQL?


A: The WHERE clause is used to filter rows at the time of querying, while the HAVING clause is used to filter groups when using GROUP BY.


Explain the difference between INNER JOIN and LEFT JOIN in SQL.


A: INNER JOIN returns only the matching rows from both tables based on the specified condition, while LEFT JOIN returns all rows from the left table and the matching rows from the right table.


Share with your family and/or friends