Based on the following Table(s), Write down the queries as indicated: 1. Write an SQL query to insert a new row in table Dept with values: 4, Prog, MO INSERT INTO DEPT VALUES(4, 'Prog','MO'); The result after executing the query is : 2. Write an SQL statement to insert a new row in table Dept with the following values: 5, Reg. Notice that location value is not given, so we will put NULL instead. INSERT INTO Dept VALUES(5, 'Reg',NULL); The Result is: 3. Write an SQL statement to Update Dept with id 2 to 20. UPDATE Dept SET id = 20 id = 2;
The result is: 4. Update the location for dept 5 to MA UPDATE dept SET location = 'MA' id = 5; The result : 5. Delete the dept with number 5; The result : DELETE FROM Dept id = 5; 6. Write an SQL statement to show all rows in Dept including all fields: SELECT * FROM Dept; or
SELECT ID, NAME, Location FROM Dept; Here, * means that I want to include all fields in my query/ 7. Write an SQL statement to show all departments with location Manama. We need to show all fields. SELECT * FROM Dept Location = 'Manama'; The result when executing the query is: 8. Let us add a new field in Table Dept. and name it as Budget of type Number ; its value as follows Now, we need to show departments with location in Manama and budget more than 800.Just we want to show the ID and Name. SELECT ID, Name FROM Dept Location ='Manama' AND Budget >800; The result when executing the query is: Change the Budget in the query above to 1500 and execute the query, How any records did you get? 9. Now we want to write an SQL query to show departments with location in Manama or budget more than 800.
ID, Name Dept ='Manama' or 800; 10. Write an SQL query to Update the Budgets of all departments by adding 1000 to each budget. UPDATE Dept SET Budget = Budget +1000; Here, Budget for all rows will be selected. The following screen shot shows the Budgets after the update: 11. Update dept with ID 3 to a new ID 30. UPDATE = ID = 3; The following shows the changes occurred in the Dept table after executing query 12. Write an SQL statement to delete all rows in Dept DELETE FROM Dept;
Don t execute this query because it will delete all Dept rows. 13. Computed columns: assume that we want to multiply each Budget by 3 without updating the Budget; just viewing. SELECT ID, Name, Location, Budget, Budget*3 FROM Dept; The result when executing the query is : as you see the result of multiplying Budget by 3 was given in a field name given by Access Expr1004. As you see the name Expr1004 is not a good name so the solution to use alias, alias is an alternative name given to a column in any SQL statement. SELECT ID, Name, Location, Budget, Budget*3 AS NewBudget FROM Dept; Note: NewBudget will not stored as a field in the Dept table. 14. The logical operator NOT Assume that we want to show information for all departments except Dept number 4. SELECT ID, Name, Location, Budget FROM Dept NOT (ID=4); The result when executing the query is :
The same above query can be done using <> operator SELECT ID, Name, Location, Budget FROM Dept ID<>4; 15. Special operators: The following operators are used in conjunction with the clause. a. BETWEEN to check whether a field value is within a range Example: Write an SQL statement to show departments with Budget between 1500 and 4000. SELECT ID, Name, Budget FROM Dept Budget BETWEEN 1500 AND 4000; The result when executing the query: b. IS NULL Used to check whether a field value is null. Example: Write an SQL statement to show departments with no Budget SELECT ID, Name, Budget FROM Dept Budget IS NULL; When executing the query, the following result comes: The reason is that all departments have budgets
Exercise: Insert a new row in Dept with values 50 for ID and Aviation for Name, leave Budget Null (empty). Remember that 0 is a value it is not NULL. Use an INSERT query to accomplish that. When you have done that, you execute the above query SELECT ID, Name, Budget FROM Dept Budget IS NULL; Did you get any records? Note: The opposite of NULL is NOT NULL. c. LIKE Used to check whether a field value matches a give string pattern. Example: Write an SQL statement to list all dept s that their location starts with M. SELECT * FROM Dept Location like 'M*'; The following result shows when executing the query: d. IN-Used to check whether a field value matches any value within a value list. Example: Write an SQL statement to show information for Dept s 1, 4, 20. SELECT * FROM Dept ID IN (1,4,20); The following result shows when executing the query:
Note: IN operator can be done using the Or operator SELECT * FROM Dept ID = 1 OR ID = 4 OR ID = 20;
ORDERING A LISTING The ORDER BY clause is used to list a query result in specific order; this order could be ascending (ASC) or descending (DESC). Syntax: SELECT field1, field2, field3, FROM table(s) condition ORDER BY field1, field2,.. [ASC DESC]; The default order is ascending means if we didn t mention ASC or DESC result will be sorted in ascending order according to the field(s) mentioned. Example(1): Write an SQL statement to show information for Dept s 1, 4, 20. SELECT * FROM Dept ID IN (1,4,20); The following result shows when executing the query: From the result you cannot till that it is any order. So, let us adjust the above query as follows: SELECT * FROM Dept ID IN (1,4,20) ORDER BY ID; Run the query:
It is clear that data returned is sorted by ID in ascending order. Note: as I mentioned above that ascending is the default. So if we added ASC to the above query we still get the same result. SELECT * FROM Dept ID IN (1,4,20) ORDER BY ID ASC; Exercise: change the data to be sorted by ID in descending order. SELECT * FROM Dept ID IN (1,4,20) ORDER BY ID DESC; The result when running the query is: Note: you can sort results using more than one field. Now, we want to introduce a new table called Employee in order to finish the rest of the chapter:
LISTING UNIQUE VALUES DISTINCT: is a clause is used to produce a list of only values that are different from one another. Example: how many different deptno s can you get from the Employee table? Assume that we issued the SQL statement : SELECT deptno FROM Employee; You get, as you see the there are all deptno s are duplicated: 20 3 times, 4 twice, 1 twice. Now let us re-write the above query by adding DISTINCT before deptno as: SELECT DISTINCT deptno FROM Employee; You get:
Aggregate Functions SQL provides functions to perform mathematical summaries such as counting the number of rows, finding the minimum or maximum values for a field, summing the values in a field, and averaging the values in a field. The following table shows basic SQL aggregate functions FUNCTION COUNT MIN MAX SUM AVG OUTPUT The number of rows containing non-null values The minimum attribute value encountered in a given column The maximum attribute value encountered in a given column The sum of all values for a given column The arithmetic mean (average) for a specified column Write an SQL statement to find the number of employees in table Employee? SELECT COUNT(empno) AS N_Employee FROM Employee; Executing this query will yield 8 rows. Let us do the COUNT on deptno as follows: SELECT COUNT(deptno) AS N_Employee FROM Employee; How many records are retrieved? Note: The syntax COUNT(*) returns the number of total rows returned by the query, including the rows that contain nulls. SELECT COUNT(*) AS N_Employee FROM Employee;
Write a query to find the maximum, minimum, average of all salaries. SELECT MAX(sal) as Highest, MIN(sal) as lowest, AVG(sal) as Average FROM Employee; Now, assume that we want to find who has the maximum salary? To answer his question we need to use what is called a sub-query SELECT empno, ename, sal FROM Employee sal = (SELECT MAX(sal) FROM Employee); MS-ACCESS, first executes the query to the right of the equal sign(=) and then it will be an input for the value of sal in the condition. Exercise Write an SQL statement to find the employee with the minimum salary GROUPING DATA Assume we want to find the total salaries paid for each department. We use the GROUP BY clause. The GROUP BY clause is valid only in conjunction with one of the SQL aggregate functions, such as COUNT, MIN, MAX, and SUM. Syntax: SELECT field(s) FROM table(s) field(s)
GROUP BY field(s); SELECT deptno, SUM(sal) as TotalSal FROM Employee GROUP BY deptno; Note: The GROUP BY clause s columnlist(field list) must include non-aggregate function columns specified in the SELECT s columnlist. Joining Database Tables Assume that you want to join the two tables Department and Employee As you see from the diagram, the deptno is the foreign key in the Employee table and the primary key in the Department table, the link established on deptno. Assume that the following data are in both tables: Department
Employee The join query is: SELECT Department.deptno, empno, Dept_Name, ename FROM Department INNER JOIN Employee ON Department.deptno = Employee.deptno; The result when executing the query is: From the result, you can note the following: 1. deptno 30 is not found in the result, why? 2. data for employee with number 129 is not found? Whay The same above query can be done using the query: SELECT Department.deptno, empno, Dept_Name, ename FROM Department, Employee Department.deptno = Employee.deptno;
Tip: start a new query as you did before then insert the Department and Employee tables as shown below: Write click and choose SQL view you get SQL code which is generated by MS-ACCESS: SELECT FROM Department INNER JOIN Employee ON Department.deptno = Employee.deptno; Here, what you have to do is write down or click what fields to be shown Left and Righ Joins Start a new query and add the Department and Employee tables to the query:
Link Double click on the link the join Properties window appears: Here you have three choices 1. if you select choice 1 then you get the matching rows between the two tables which is inner join that we performed before: 22. as choice 1 is selected click OK then add the fileds that you want to show in the query result as shown below:
The SQL code is: SELECT Department.deptno, Dept_Name, empno,ename FROM Department INNER JOIN Employee ON Department.deptno = Employee.deptno; The result when executing the query is : 23. Sart a new query and insert the two tables as in above and double click on the link and select the second choice:
Here the result will show all Deprtment records whether or not they match with records in Employee table. The SQL code is: SELECT Department.Dept_id, Employee.empno, Department.Dept_Name, Employee.ename FROM Department LEFT JOIN Employee ON Department.Dept_id = Employee.deptno; The result is:
Note that dept id 40 is shown even there is employees working for this department. 24. Do as in the above and select the the third choice: Assume we have an employee with no deptno as shown below in the Employee table: The SQL code is: SELECT Department.Dept_id, Employee.empno, Department.Dept_Name, Employee.ename
FROM Department RIGHT JOIN Employee ON Department.Dept_id = Employee.deptno; The result is : Note that employee sameeh with no Deptid.