Software Development Exam  >  Software Development Notes  >  SQL for Beginners  >  Assignment: SQL Practice Questions

Assignment: SQL Practice Questions | SQL for Beginners - Software Development PDF Download

Q1: Write an SQL query to find customers who have placed more than 3 orders in the last 6 months. Return the customer’s customer_id and the total number of orders they placed.

Assignment: SQL Practice Questions | SQL for Beginners - Software DevelopmentSol: 
SELECT customer_id, COUNT(order_id) AS total_orders
FROM Orders
WHERE order_date >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH)
GROUP BY customer_id
HAVING total_orders > 3;

Q2:  Write an SQL query to return the employee_id and percentage increase in salary for employees who received a salary increase compared to their previous salary.
Tables:

Assignment: SQL Practice Questions | SQL for Beginners - Software Development
Sol:
SELECT s1.employee_id,
    ROUND(((s2.salary - s1.salary) / s1.salary) * 100, 2) AS percentage_increase
FROM Salaries s1
JOIN Salaries s2
    ON s1.employee_id = s2.employee_id
    AND s1.salary_date < s2.salary_date
WHERE s2.salary > s1.salary;

Q3:  Find the top 3 users with the highest number of logins in the last 30 days.
Tables:

Assignment: SQL Practice Questions | SQL for Beginners - Software Development

Sol: 
SELECT user_id, COUNT(*) AS login_count
FROM UserLogins
WHERE login_date >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)
GROUP BY user_id
ORDER BY login_count DESC
LIMIT 3;

Q4: Find transactions where the same user made identical purchases (same amount and transaction_date) more than once.
Sol: 
SELECT user_id, amount, transaction_date, COUNT(*) AS duplicate_count
FROM Transactions
GROUP BY user_id, amount, transaction_date
HAVING duplicate_count > 1;

Q5: Write an SQL query to calculate the cumulative sales for each product by date.
Tables:

Assignment: SQL Practice Questions | SQL for Beginners - Software DevelopmentSol:
SELECT product_id, sale_date, amount,
     SUM(amount) OVER (PARTITION BY product_id ORDER BY sale_date) AS running_total
FROM Sales;

The document Assignment: SQL Practice Questions | SQL for Beginners - Software Development is a part of the Software Development Course SQL for Beginners.
All you need of Software Development at this link: Software Development
1 videos|10 docs

FAQs on Assignment: SQL Practice Questions - SQL for Beginners - Software Development

1. What are the basic SQL commands used for data manipulation?
Ans. The basic SQL commands used for data manipulation are SELECT, INSERT, UPDATE, and DELETE. SELECT is used to retrieve data from a database, INSERT is used to add new records, UPDATE modifies existing records, and DELETE removes records from a table.
2. How can I join two tables in SQL?
Ans. You can join two tables in SQL using the JOIN clause, which allows you to combine rows from both tables based on a related column. The most common types of joins are INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN. For example, to perform an INNER JOIN, you would use: `SELECT * FROM Table1 INNER JOIN Table2 ON Table1.id = Table2.id;`.
3. What is the difference between primary key and foreign key in SQL?
Ans. A primary key is a unique identifier for a record in a table, ensuring that no two rows have the same value in that column. A foreign key, on the other hand, is a column that creates a link between two tables. It refers to the primary key in another table, establishing a relationship between the two tables.
4. How do I filter records in SQL?
Ans. You can filter records in SQL using the WHERE clause. This clause allows you to specify conditions that the retrieved records must meet. For example, `SELECT * FROM Employees WHERE department = 'Sales';` filters the records to show only those where the department is 'Sales'.
5. What is normalization in SQL and why is it important?
Ans. Normalization is the process of organizing data in a database to reduce redundancy and improve data integrity. It involves dividing a database into tables and establishing relationships between them. Normalization is important because it helps to eliminate duplicate data and ensures consistency, making the database more efficient and easier to manage.
1 videos|10 docs
Download as PDF

Top Courses for Software Development

Related Searches

Extra Questions

,

video lectures

,

Free

,

practice quizzes

,

Assignment: SQL Practice Questions | SQL for Beginners - Software Development

,

past year papers

,

study material

,

Summary

,

mock tests for examination

,

Important questions

,

Objective type Questions

,

MCQs

,

pdf

,

Sample Paper

,

Assignment: SQL Practice Questions | SQL for Beginners - Software Development

,

Assignment: SQL Practice Questions | SQL for Beginners - Software Development

,

Exam

,

shortcuts and tricks

,

Viva Questions

,

ppt

,

Semester Notes

,

Previous Year Questions with Solutions

;