SQL Code Questions
SQL Code Questions
10. Write a SQL statement to create a table named jobs including columns
job_id, job_title, min_salary and max_salary, and make sure that, the default
value for job_title is blank and min_salary is 8000 and max_salary is NULL will
be entered automatically at the time of insertion if no value assigned for the
specified columns.
11. Write a SQL statement to create a table named countries including columns
country_id, country_name and region_id and make sure that the country_id
column will be a key field which will not contain any duplicate data at the time
of insertion.
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| JOB_ID | varchar(10) | NO | PRI | | |
| JOB_TITLE | varchar(35) | NO | | NULL | |
| MIN_SALARY | decimal(6,0) | YES | | NULL | |
| MAX_SALARY | decimal(6,0) | YES | | NULL | |
+------------+--------------+------+-----+---------+-------+
+-----------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| DEPARTMENT_ID | decimal(4,0) | NO | PRI | 0 | |
| DEPARTMENT_NAME | varchar(30) | NO | | NULL | |
| MANAGER_ID | decimal(6,0) | NO | PRI | 0 | |
| LOCATION_ID | decimal(4,0) | YES | | NULL | |
+-----------------+--------------+------+-----+---------+-------+
+-----------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| DEPARTMENT_ID | decimal(4,0) | NO | PRI | 0 | |
| DEPARTMENT_NAME | varchar(30) | NO | | NULL | |
| MANAGER_ID | decimal(6,0) | YES | | NULL | |
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| JOB_ID | varchar(10) | NO | PRI | | |
| JOB_TITLE | varchar(35) | NO | | NULL | |
| MIN_SALARY | decimal(6,0) | YES | | NULL | |
| MAX_SALARY | decimal(6,0) | YES | | NULL | |
+------------+--------------+------+-----+---------+-------+
Assume that the structure of the table jobs and InnoDB Engine have been
used to create the table jobs.
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| JOB_ID | int(11) | NO | PRI | NULL | |
| JOB_TITLE | varchar(35) | NO | | | |
| MIN_SALARY | decimal(6,0) | YES | | 8000 | |
| MAX_SALARY | decimal(6,0) | YES | | NULL | |
+------------+--------------+------+-----+---------+-------+
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| JOB_ID | int(11) | NO | PRI | NULL | |
| JOB_TITLE | varchar(35) | NO | | | |
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| JOB_ID | int(11) | NO | PRI | NULL | |
| JOB_TITLE | varchar(35) | NO | | | |
| MIN_SALARY | decimal(6,0) | YES | | 8000 | |
| MAX_SALARY | decimal(6,0) | YES | | NULL | |
+------------+--------------+------+-----+---------+-------+
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| JOB_ID | int(11) | NO | PRI | NULL | |
| JOB_TITLE | varchar(35) | NO | | | |
| MIN_SALARY | decimal(6,0) | YES | | 8000 | |
| MAX_SALARY | decimal(6,0) | YES | | NULL | |
+------------+--------------+------+-----+---------+-------+
+--------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------+------+-----+---------+-------+
| COUNTRY_ID | varchar(2) | YES | | NULL | |
| COUNTRY_NAME | varchar(40) | YES | | NULL | |
| REGION_ID | decimal(10,0) | YES | | NULL | |
+--------------+---------------+------+-----+---------+-------+
2. Write a SQL statement to insert one row into the table countries against the
column country_id and country_name.
Here in the following is the structure of the table countries.
+--------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------+------+-----+---------+-------+
| COUNTRY_ID | varchar(2) | YES | | NULL | |
| COUNTRY_NAME | varchar(40) | YES | | NULL | |
| REGION_ID | decimal(10,0) | YES | | NULL | |
+--------------+---------------+------+-----+---------+-------+
4. Write a SQL statement to insert NULL values against region_id column for a
row of countries table.
+------------+--------------+-----------+
| COUNTRY_ID | COUNTRY_NAME | REGION_ID |
+------------+--------------+-----------+
| C0001 | India | 1001 |
| C0002 | USA | 1007 |
| C0003 | UK | 1003 |
+------------+--------------+-----------+
8. Write a SQL statement to insert one row in jobs table to ensure that no
duplicate value will be entered in the job_id column.
9. Write a SQL statement to insert a record into the table countries to ensure
that, a country_id and region_id combination will be entered once in the table.
11. Write a SQL statement to insert records into the table countries to ensure
that the country_id column will not contain any duplicate data and this will be
automatically incremented and the column country_name will be filled up by
'N/A' if no value assigned for that column.
12. Write a SQL statement to insert rows in the job_history table in which one
column job_id is containing those values which are exists in job_id column of
jobs table.
+--------+-----------+------------+------------+
| JOB_ID | JOB_TITLE | MIN_SALARY | MAX_SALARY |
+--------+-----------+------------+------------+
| 1001 | OFFICER | 8000 | 20000 |
| 1002 | CLERK | 8000 | 20000 |
+--------+-----------+------------+------------+
2 rows in set (0.00 sec)
The value against job_id is 1001 which is exists in the job_id column of the jobs
table, so no problem arise.
Here in the above, the value against department_id and manager_id combination
(211,80) does not matching with the same combination in departments(parent table)
table and that is why the child table employees can not contain the combination of
Here in the above insert statement the child column department_id and job_id of
child table employees are successfully referencing with the department_id and
job_id column of parent tables departments and jobs respectively, so no problem
have been arisen to the insertion.
Here in the above insert statement show that, within child columns department_id
and job_id of child table employees, the job_id are successfully referencing with the
job_id of parent table jobs but department_id column are not successfully
referencing with the department_id of parent table departments, so the problem have
been arisen to the insertion and displayed the error message.
1. Write a SQL statement to change the email column of employees table with
'not available' for all employees.
4. Write a SQL statement to change the email column of employees table with
'not available' for those employees whose department_id is 80 and gets a
commission is less than .20%
See the result. Only the effected rows have been displayed.
5. Write a SQL statement to change the email column of employees table with
'not available' for those employees who belongs to the 'Accouning'
department.
+---------------+----------------------+------------+-------------+
| DEPARTMENT_ID | DEPARTMENT_NAME | MANAGER_ID | LOCATION_ID |
+---------------+----------------------+------------+-------------+
| 10 | Administration | 200 | 1700 |
| 20 | Marketing | 201 | 1800 |
| 30 | Purchasing | 114 | 1700 |
| 40 | Human Resources | 203 | 2400 |
| 50 | Shipping | 121 | 1500 |
| 60 | IT | 103 | 1400 |
| 70 | Public Relations | 204 | 2700 |
UPDATE employees
SET email='not available'
WHERE department_id=(
SELECT department_id
FROM departments
WHERE department_name='Accounting');
See the result. Only the effected rows have been displayed.
See the result. Only the effected rows have been displayed.
See the result. Only the effected rows have been displayed.
See the result before update. Only the effected rows have been displayed.
+------------+---------------------------------+------------+------------+
| JOB_ID | JOB_TITLE | MIN_SALARY | MAX_SALARY |
+------------+---------------------------------+------------+------------+
| AD_PRES | President | 20000 | 40000 |
| AD_VP | Administration Vice President | 15000 | 30000 |
| AD_ASST | Administration Assistant | 3000 | 6000 |
| FI_MGR | Finance Manager | 8200 | 16000 |
| FI_ACCOUNT | Accountant | 4200 | 9000 |
| AC_MGR | Accounting Manager | 8200 | 16000 |
| AC_ACCOUNT | Public Accountant | 4200 | 9000 |
| SA_MAN | Sales Manager | 10000 | 20000 |
| SA_REP | Sales Representative | 6000 | 12000 |
| PU_MAN | Purchasing Manager | 8000 | 15000 |
| PU_CLERK | Purchasing Clerk | 2500 | 5500 |
| ST_MAN | Stock Manager | 5500 | 8500 |
| ST_CLERK | Stock Clerk | 2000 | 5000 |
| SH_CLERK | Shipping Clerk | 2500 | 5500 |
| IT_PROG | Programmer | 4000 | 10000 |
| MK_MAN | Marketing Manager | 9000 | 15000 |
| MK_REP | Marketing Representative | 4000 | 9000 |
| HR_REP | Human Resources Representative | 4000 | 9000 |
| PR_REP | Public Relations Representative | 4500 | 10500 |
+------------+---------------------------------+------------+------------+
UPDATE jobs,employees
SET jobs.min_salary=jobs.min_salary+2000,
See the result before update. Only the effected rows have been displayed.
See the result. Only the effected rows have been displayed.