SQL Queries for PracticeIf your objective is to be a data administrator, database developer, or data analyst, then it is important to master SQL queries. If you want to work with structured data then you must know how to retrieve, handle, and analyze data from relational databases. In this article, we will learn several SQL queries that will help you increase your querying abilities. Following are the SQL queries: 1. Write the query to create a new table. 2. Write the query to insert a new record into a table: 3. Write the query to delete records from a table: 4. Write the query to update records in a table: 5. Write the query to delete a table from the database: 6. Write the query to select all records from a table to retrieve data: 7. Write the query to select the specific column from a table: 8. Write the query to select distinct values from a column: 9. Write the query to filter records using a WHERE clause: 10. Write the query to sort records in descending order: 11. Write the query to sort records in ascending order: 12. Write the query to join two tables based on a common column: 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: 14. Write the query to group records and calculate aggregate functions: 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: 16. Write the query to find the sum of values in a column: 17. Write the query to find the average value of a column: 18. Write the query to get the minimum value from a column: 19. Write the query to retrieve the maximum value from a column: 20. Write the query to retrieve rows with values within a specified range: 21. Write the query to retrieve rows with values matching a list of specified values: 22. Write the query to search for patterns in a column using wildcard characters: 23. Write the query to filter data based on aggregate functions in a GROUP BY query: 24. Write the query to retrieve rows with dates within a specified range: 25. Write the query to combine the result sets of two or more SELECT statements: 26. Write the query to perform conditional logic in a query: 27. Write the query to delete all rows from a table, but keeps the table structure: 28. Write the query to create an index on one or more columns of a table: 29. Write the query to modify the structure of an existing table: 30. Write the query to return the first non-null value from a list of expressions: 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: Real-life-based questions to practice SQL queries:Let us look at some real-life based questions to practice the SQL queries discussed above: 1. Write the query to create a table named 'employees' with fields like empId, empName, empAge, empAddress, and empSalary. Query: Result: The 'employees' table will look like you can see below.
2. Write the query to insert values in the above 'employees' table. Query: Result: After inserting values in the 'employees' table, it will look like you can see below.
3. Consider the 'employees' table and write the query to delete record of 'empId=1' from the 'employees' table: Query: Result: The above statement will delete the record of 'empId=1' from the 'employees' table.
4. Consider the 'employees' table and write the query to update the age of 'empId=5' in the 'employees' table: Query: Result: The above statement will update the age of 'empId=5' in the 'employees' table.
5. Consider the 'employees' table and write the query to select all records from the 'employees' table: Query: Result: As you can see below, above statement will select all records from the 'employees' table.
6. Consider the 'employees' table and write the query to select the 'empName' and 'empSalary' columns from the 'employees' table: Query: Result: The above statement will select the 'empName' and 'empSalary' columns from the 'employees' table.
7. Consider the 'employees' table and write the query to select distinct values from the 'empAddress' column in the 'employees' table: Query: Result: The above statement will select the distinct value from the 'empAddress' column in the 'employees' table.
8. Consider the 'employees' table and write the query to filter and select those records where empAddress='Jaipur' using a WHERE clause: Query: Result: The above statement will filter the records from the 'employees' table.
9. Consider the 'employees' table and write the query to sort records of the 'employees' table in descending order. Query: Result: The above statement will sort the records of the 'employees' table in descending order.
10. Consider the 'employees' table and write the query to sort records of the 'employees' table in ascending order. Query: Result: The above statement will sort the records of the 'employees' table in ascending order.
11. Consider the 'employees' table and write the query to count the number of rows in the 'employees' table. Query: Result: The above statement will count the number of rows in the 'employees' table.
12. Consider the 'employees' table and write the query to retrieve all employees whose salary is between 35000 and 40000 in the 'employees' table. Query: Result: The above statement will retrieve all employees whose salary is between 35000 and 40000 in the 'employees' table.
13. Consider the 'employees' table and write the query to add another column called 'Department'. Query: Result: As no value has been inserted in the new column 'Department' so by default it contains NULL.
14. Consider a table called 'products' with fields such as product_id, product_name, product_quantity, and product_price.
i) Write the query to calculate the sum of values in the 'product_price' column in the 'products' table. Query: Result:
ii) Write the query to calculate the average value of the 'product_price' column in the 'products' table. Query: Result:
iii) Write the query to retrieve the minimum value from the 'product_price' column in the 'products' table. Query: Result:
iv) Write the query to retrieve the maximum value from a 'product_price' column in the 'products' table. Query: Result:
V) Write the query to group records and calculate aggregate functions for the 'product_price' column in the 'products' table. Query: Result:
15. Consider a table called 'mobilephones' with fields such as Id, Name, Company, Quantity, and Price.
i) Write the query to select all customers from a 'Colour' column starting with "bl" in the 'mobilephones' table. Query: Result:
15. Consider a table called 'mobilephones' with fields such as Id, Name, Company, Quantity, and Price.
ii) Write the query to select all mobile names whose average is greater than 45000 from the 'Price' column in the 'mobilephones' table. Query: Result:
iii) Write the query to select all mobile names whose sum is smaller than 45000 from a 'Price' column in the 'mobilephones' table. Query: Result:
16. Consider the 'customers' table with Id, firstName, middleName, lastName, and Address.
i) Write the query to return the first non-null value from the firstName, middleName, and lastName columns. Query: Result:
ii) Write the query to delete all rows from a 'customers' table, but keeps the table structure. Query: Result: As you can see, the above statement has deleted all rows but left with only the table structure.
17. Consider the two tables: one is the 'teachers' table which has fields such as teachers_id, Name, teachers_age, and teachers_address. The other is the 'students' table which has fields such as students_id, Name, students_age, and students_address. The following is the 'teachers' table:
The following is the 'students' table:
i) Write the query to combine the 'Name' column in the 'teachers' table with the 'Name' column in the 'students' table using the UNION operator. Query: Result:
ii) Write the query to find the teacher's name whose age is between 34 and 41 in the 'teachers' table. Query: Result:
Conclusion:In this article, you have learned about several SQL queries for practice. You have read various real-life-based questions and used SQL queries to solve them. Practicing SQL queries is important if you want to become proficient at managing and retrieving data from a database. With regular practice, you can understand SQL syntax properly, and improve query optimization techniques. You can easily identify and correct errors. If you master SQL queries, you will be able to extract valuable insights from databases and make data-driven decisions that will help advance your career.
Next TopicNested Query In SQL
|