Create

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

1. Write the query to create a new table.

CREATE TABLE tableName (columnName datatype, columnName datatype, ... );

2. Write the query to insert a new record into a table:

INSERT INTO tableName (Column1, Column2, ?) VALUES (Value1, Value2, ?);

3. Write the query to delete records from a table:

DELETE FROM tableName WHERE condition;

4. Write the query to update records in a table:

UPDATE tableName SET columnName = newValue WHERE condition;

5. Write the query to delete a table from the database:

DROP TABLE tableName;

6. Write the query to select all records from a table to retrieve data:

SELECT * FROM tableName;


7. Write the query to select the specific column from a table:

SELECT Column1, Column2 FROM tableName;

8. Write the query to select distinct values from a column:

SELECT DISTINCT columnName FROM tableName;

9. Write the query to filter records using a WHERE clause:

SELECT * FROM tableName WHERE condition;

10. Write the query to sort records in descending order:

SELECT * FROM tableName ORDER BY columnName DESC;

11. Write the query to sort records in ascending order:

SELECT * FROM tableName ORDER BY columnName ASC;


12. Write the query to join two tables based on a common column:

SELECT * FROM Table1 JOIN Table2 ON Table1.columnName = Table2.columnNam


e;

13. Write the query to count the number of rows in a table.

We will use the COUNT() function to count the number of rows. The statement is given
below:
SELECT COUNT(*) FROM tableName;

14. Write the query to group records and calculate aggregate functions:

SELECT columnName, COUNT(*), AVG(columnName) FROM tableName GROUP BY


columnName;

15. Write the query to limit the number of rows returned in a result set.

We will use the LIMIT clause to limit the number of rows. The statement is given below:

SELECT columnName FROM tableName LIMIT 10;

16. Write the query to find the sum of values in a column:

SELECT SUM(columnName) FROM tableName;

17. Write the query to find the average value of a column:

SELECT AVG(columnName) FROM tableName;


18. Write the query to get the minimum value from a column:

SELECT MIN(columnName) FROM tableName;

19. Write the query to retrieve the maximum value from a column:

SELECT MAX(columnName) FROM tableName;

20. Write the query to retrieve rows with values within a specified range:

SELECT columnName FROM tableName WHERE columnName BETWEEN Value1 A


ND Value2;
21. Write the query to retrieve rows with values matching a list of specified values:

SELECT columnName FROM tableName WHERE columnName


IN (Value1, Value2, Value3);

22. Write the query to search for patterns in a column using wildcard characters:

SELECT columnName FROM tableName WHERE columnName LIKE 'abc%';

23. Write the query to filter data based on aggregate functions in a GROUP BY query:

SELECT Column1, COUNT(Column2) FROM tableName GROUP BY Column1 HAVIN


G COUNT(Column2) > 5;
24. Write the query to retrieve rows with dates within a specified range:

SELECT columnName FROM tableName WHERE columnName BETWEEN '2023-01-


01' AND '2023-07-30';

25. Write the query to combine the result sets of two or more SELECT statements:

SELECT * FROM Table1 UNION SELECT columnName FROM Table2;

26. Write the query to perform conditional logic in a query:

SELECT columnNamse, CASE WHEN Condition1 THEN 'Value1' WHEN Condition2


THEN 'Value2' ELSE 'Value3' END AS newColumn FROM tableName;

27. Write the query to delete all rows from a table, but keeps the table structure:

TRUNCATE TABLE tableName;

28. Write the query to create an index on one or more columns of a table:

CREATE INDEX indexName ON tableName (Column1, Column2);

29. Write the query to modify the structure of an existing table:

ALTER TABLE tableName ADD columnName datatype;


30. Write the query to return the first non-null value from a list of expressions:

SELECT COALESCE(Column1, Column2,.. ColumnN) AS Result FROM tableName;


31. Write the query to return null if two expressions are equal, otherwise, return the first
expression.

We will use the NULLIF() function to do so, the statement is given below:

SELECT NULLIF(Column1, 0) AS result FROM tableName;

Real-life based questions for each of the SQL queries:


1. Create a new table:
- As a database administrator for a retail company, how would you design a table to store
customer information, including their name, email, and phone number?

2. Insert a new record into a table:


- Imagine you're a sales representative entering a new customer's data into the CRM system. Walk
me through the process of inserting this information into the database.

3. Delete records from a table:


- In an online bookstore, how would you delete all customer reviews that were flagged as spam or
inappropriate?

4. Update records in a table:


- As an inventory manager for a grocery store, how would you update the quantity of a product in
the database after receiving a new shipment?

5. Delete a table from the database:


- Suppose you're decommissioning an old feature in a software application. Explain the process of
removing the corresponding table from the database.

6. Select all records from a table to retrieve data:


- As a customer service representative, how would you retrieve all orders placed by a specific
customer from the database?

7. Select specific column from a table:


- In a hospital's patient management system, how would you retrieve the names and ages of all
patients currently admitted?

8. Select distinct values from a column:


- In an online forum, how would you retrieve a list of unique usernames from the database to
display on the registration page?

9. Filter records using a WHERE clause:


- As a financial analyst, how would you retrieve all transactions above $1000 made by a specific
client from the banking database?

10. Sort records in descending order:


- In an e-commerce platform, how would you display a list of products sorted by price from
highest to lowest?

11. Sort records in ascending order:


- As a teacher, how would you retrieve a list of student grades sorted alphabetically by their last
names?

12. Join two tables based on a common column:


- In a university database, how would you retrieve a list of students along with their respective
majors by joining the "students" and "majors" tables?

13. Count the number of rows in a table:


- As an IT manager, how would you determine the total number of active user accounts in the
company's system?

14. Group records and calculate aggregate functions:


- In a sales database, how would you calculate the total revenue generated by each product
category?

15. Limit the number of rows returned in a result set:


- As a news website administrator, how would you display only the 10 most recent articles on the
homepage?

16. Find the sum of values in a column:


- In a financial application, how would you calculate the total balance across all bank accounts
held by a specific client?

17. Find the average value of a column:


- In a survey database, how would you calculate the average satisfaction rating given by customers
for a particular product?

18. Get the minimum value from a column:


- In a temperature monitoring system, how would you determine the coldest temperature
recorded in a specific location?

19. Retrieve the maximum value from a column:


- In an online gaming platform, how would you find the highest score achieved by any player in a
particular game?

20. Retrieve rows with values within a specified range:


- As a real estate agent, how would you retrieve a list of properties with prices between $200,000
and $300,000 from the database?

21. Retrieve rows with values matching a list of specified values:


- In a product inventory system, how would you retrieve a list of products with specific SKUs
provided by the warehouse manager?

22. Search for patterns in a column using wildcard characters:


- In a customer database, how would you retrieve email addresses of all customers whose emails
start with "john" for a targeted email campaign?

23. Filter data based on aggregate functions in a GROUP BY query:


- In a restaurant's reservation system, how would you retrieve a list of tables that have been
reserved more than 5 times in the past month?

24. Retrieve rows with dates within a specified range:


- In a scheduling application, how would you retrieve a list of appointments scheduled between
January 1st, 2023, and July 30th, 2023?

25. Combine the result sets of two or more SELECT statements:


- In a social media platform, how would you merge the lists of followers and followings for a
user's profile page?

26. Perform conditional logic in a query:


- In an HR database, how would you categorize employees into different salary brackets based on
their years of experience?

27. Delete all rows from a table, but keep the table structure:
- In a testing environment, how would you clear out all test data from the "user" table before
running a new round of tests?

28. Create an index on one or more columns of a table:


- In a large dataset of customer orders, how would you improve query performance by creating an
index on the "customer_id" column?

29. Modify the structure of an existing table:


- In a blogging platform, how would you add a new column to the "posts" table to track the
number of views each post receives?

30. Return the first non-null value from a list of expressions:


- In a survey database, how would you display the primary language spoken by respondents,
prioritizing their preferred language if available?

31. Return null if two expressions are equal, otherwise, return the first expression:
- In a voting system, how would you handle cases where two candidates receive the same number
of votes, returning null to indicate a tie?

Answers:

1. Create a new table:


- Answer: To create a table to store customer information, you would execute the SQL query:
`CREATE TABLE customers (customer_id INT PRIMARY KEY, name VARCHAR(255), email
VARCHAR(255), phone VARCHAR(20));`

2. Insert a new record into a table:


- Answer: As a sales representative, you would execute the SQL query to insert a new customer's
data: `INSERT INTO customers (name, email, phone) VALUES ('John Doe',
'john@example.com', '+1234567890');`

3. Delete records from a table:


- Answer: To delete all flagged customer reviews from the database, you would execute the SQL
query: `DELETE FROM reviews WHERE flagged = 'spam' OR flagged = 'inappropriate';`

4. Update records in a table:


- Answer: As an inventory manager, to update the quantity of a product after receiving a new
shipment, you would execute the SQL query: `UPDATE products SET quantity = quantity + 100
WHERE product_id = 'ABC123';`

5. Delete a table from the database:


- Answer: To remove an old feature's corresponding table from the database, you would execute
the SQL query: `DROP TABLE old_feature_data;`

6. Select all records from a table to retrieve data:


- Answer: As a customer service representative, to retrieve all orders placed by a specific
customer, you would execute the SQL query: `SELECT * FROM orders WHERE customer_id =
'12345';`

7. Select specific column from a table:


- Answer: In a hospital's patient management system, to retrieve the names and ages of all
patients currently admitted, you would execute the SQL query: `SELECT name, age FROM patients
WHERE admitted = true;`

8. Select distinct values from a column:


- Answer: In an online forum, to retrieve a list of unique usernames from the database, you would
execute the SQL query: `SELECT DISTINCT username FROM users;`

9. Filter records using a WHERE clause:


- Answer: As a financial analyst, to retrieve all transactions above $1000 made by a specific client,
you would execute the SQL query: `SELECT * FROM transactions WHERE amount > 1000 AND
client_id = 'ABC123';`

10. Sort records in descending order:


- Answer: In an e-commerce platform, to display a list of products sorted by price from highest to
lowest, you would execute the SQL query: `SELECT * FROM products ORDER BY price DESC;`

11. Sort records in ascending order:


- Answer: As a teacher, to retrieve a list of student grades sorted alphabetically by their last
names, you would execute the SQL query: `SELECT * FROM grades ORDER BY last_name ASC;`

12. Join two tables based on a common column:


- Answer: In a university database, to retrieve a list of students along with their respective majors,
you would execute the SQL query: `SELECT students.name, majors.major FROM students INNER
JOIN majors ON students.major_id = majors.major_id;`

13. Count the number of rows in a table:


- Answer: As an IT manager, to determine the total number of active user accounts in the
company's system, you would execute the SQL query: `SELECT COUNT(*) FROM users WHERE
status = 'active';`

14. Group records and calculate aggregate functions:


- Answer: In a sales database, to calculate the total revenue generated by each product category,
you would execute the SQL query: `SELECT category, SUM(revenue) FROM sales GROUP BY
category;`

15. Limit the number of rows returned in a result set:


- Answer: As a news website administrator, to display only the 10 most recent articles on the
homepage, you would execute the SQL query: `SELECT * FROM articles ORDER BY publish_date
DESC LIMIT 10;`

16. Find the sum of values in a column:


- Answer: In a financial application, to calculate the total balance across all bank accounts held by
a specific client, you would execute the SQL query: `SELECT SUM(balance) FROM accounts
WHERE client_id = 'ABC123';`

17. Find the average value of a column:


- Answer: In a survey database, to calculate the average satisfaction rating given by customers for
a particular product, you would execute the SQL query: `SELECT AVG(satisfaction_rating) FROM
feedback WHERE product_id = 'XYZ789';`

18. Get the minimum value from a column:


- Answer: In a temperature monitoring system, to determine the coldest temperature recorded in
a specific location, you would execute the SQL query: `SELECT MIN(temperature) FROM readings
WHERE location = 'City A';`

19. Retrieve the maximum value from a column:


- Answer: In an online gaming platform, to find the highest score achieved by any player in a
particular game, you would execute the SQL query: `SELECT MAX(score) FROM game_scores
WHERE game_id = '123';`

20. Retrieve rows with values within a specified range:


- Answer: As a real estate agent, to retrieve a list of properties with prices between $200,000 and
$300,000 from the database, you would execute the SQL query: `SELECT * FROM properties
WHERE price BETWEEN 200000 AND 300000;`

21. Retrieve rows with values matching a list of specified values:


- Answer: In a product inventory system, to retrieve a list of products with specific SKUs provided
by the warehouse manager, you would execute the SQL query: `SELECT * FROM products WHERE
sku IN ('SKU123', 'SKU456', 'SKU789');`

22. Search for patterns in a column using wildcard characters:


- Answer: In a customer database, to retrieve email addresses of all customers whose emails start
with "john" for a targeted email campaign, you would execute the SQL query: `SELECT email FROM
customers WHERE email LIKE 'john%';`

23. Filter data based on aggregate functions in a GROUP BY query:


- Answer: In a restaurant's reservation system, to retrieve a list of tables that have been reserved
more than 5 times in the past month, you would execute the SQL query: `SELECT table_id FROM
reservations GROUP BY table_id HAVING COUNT(*) > 5;`

24. Retrieve rows with dates within a specified range:


- Answer: In a scheduling application, to retrieve a list of appointments scheduled between
January 1st, 2023, and July 30th, 2023, you would execute the SQL query: `SELECT * FROM
appointments WHERE appointment_date BETWEEN '2023-01-01' AND '2023-07-30';`

25. Combine the result sets of two or more SELECT statements:


- Answer: In a social media platform, to merge the lists of followers and followings for a user's
profile page, you would execute the SQL query: `SELECT * FROM followers WHERE user_id = '123'
UNION SELECT * FROM followings WHERE user_id = '123';`

26. Perform conditional logic in a query:


- Answer: In an HR database, to categorize employees into different salary brackets based

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy