Employee Table Creation in SQL
Employee Table Creation in SQL
To find the employee with the maximum gross salary, you first need to get the maximum gross value using the MAX() function, then filter the employee records. The query could be written as: SELECT Name FROM Employee WHERE Gross=(SELECT Max(Gross) FROM Employee); This uses a subquery to first determine the maximum gross salary, which is then used to filter out the specific employee(s) with that gross value .
To list employees by department with their names in ascending order, you can combine the ORDER BY clause in SQL with multiple sorting criteria. The query would be: SELECT Name, Department FROM Employee ORDER BY Department, Name; This command sorts the employees first by 'Department' and then each department's employees by 'Name' in ascending order .
To display employee details sorted by their gross salary in descending order, you use the SQL SELECT statement combined with the ORDER BY clause. The exact command would be: SELECT * FROM Employee ORDER BY Gross DESC; This command sorts the rows in the 'Employee' table by the 'Gross' column in descending order .
The SQL query to determine the average gross salary of managers would use the AVG() function. The query is: SELECT AVG(Gross) FROM Employee WHERE Designation='manager'; This command computes the average of the 'Gross' column values filtered by the 'Designation' specified as 'manager' .
SQL can update a column with calculated values from other columns using the UPDATE statement combined with SET. The process involves specifying the target column, then assigning it a formula that calculates the value using other columns. For instance, updating a 'Total' column with sum values from 'sub1', 'sub2', and 'sub3', the SQL command is: UPDATE student SET Total=sub1+sub2+sub3; This demonstrates SQL's ability to handle arithmetic operations for column updates .
To calculate the total salary expenditure for each department, you use the SQL SUM() function along with GROUP BY. The query is: SELECT Sum(Gross), Department FROM Employee GROUP BY Department; This command calculates the sum of the 'Gross' salaries and groups the results by 'Department' .
To retrieve a sorted list of student names in ascending order, you would use the ORDER BY clause. The SQL statement would be: SELECT Name FROM student ORDER BY Name; This command selects the 'Name' column from the 'student' table and sorts the results in ascending order by name .
To find the highest mark recorded for the second subject (sub2) in a student table, you use the SQL aggregate function MAX(). The query is: SELECT Max(sub2) FROM student; This command retrieves the maximum value in the 'sub2' column .
To update a column with the total marks calculated from individual subject scores, you use the SQL UPDATE statement. In this case, for the 'student' table, the command would be: UPDATE student SET Total=sub1+sub2+sub3; This updates the 'Total' column by summing the values of 'sub1', 'sub2', and 'sub3' for each record .
SQL uses the GROUP BY clause to perform operations on subsets of data divided into groups based on one or more columns. For aggregation tasks, it allows you to apply functions like SUM, AVG, MAX to grouped records, producing a single result per group. An example is calculating the total gross salary per department: SELECT Sum(Gross), Department FROM Employee GROUP BY Department; Here, the SQL statement computes the total salary ('Sum(Gross)') for each 'Department', demonstrating the use of GROUP BY to aggregate data on a group basis .