0% found this document useful (0 votes)
71 views9 pages

Assign 5 SQL

The document contains SQL queries and output from creating, populating, and querying various database tables. The key points are: 1) The queries create tables for employees, nations, and inventions, and populate them with data. 2) Information is retrieved from the tables through SELECT queries with various WHERE, ORDER BY, and other clauses. 3) The tables are modified through INSERT, UPDATE, DELETE, ALTER, and DROP queries.

Uploaded by

Gagan Khorwal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
71 views9 pages

Assign 5 SQL

The document contains SQL queries and output from creating, populating, and querying various database tables. The key points are: 1) The queries create tables for employees, nations, and inventions, and populate them with data. 2) Information is retrieved from the tables through SELECT queries with various WHERE, ORDER BY, and other clauses. 3) The tables are modified through INSERT, UPDATE, DELETE, ALTER, and DROP queries.

Uploaded by

Gagan Khorwal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 9

ASSIGNMENT-5

DateQUERIESmysql> create table emp(empno integer(4) primary key,ename varchar(25),job varch


ar(10),mgr integer(4),hiredate date,sal integer(7),comm integer(7),deptno integer(2));
Query OK, 0 rows affected (0.73 sec)
mysql> insert into emp values(056,'aman','ceo',1000,'1995-9-5',5000000,400000,2);
Query OK, 1 row affected (0.07 sec)
mysql> insert into emp values(057,'aayush','manager',100,'1999-9-6',50000,400000,4);
Query OK, 1 row affected (0.05 sec)
mysql> insert into emp values(004,'akshay','clerk',100,'1999-6-6',30000,40000,1);
Query OK, 1 row affected (0.05 sec)
mysql> insert into emp values(123,'baba','salesman',001,'1998-6-4',3000,400,3);
Query OK, 1 row affected (0.07 sec)
mysql> select*from emp;

OUTPUT
+-------+--------+----------+------+------------+---------+--------+--------+
| empno | ename | job | mgr | hiredate | sal
| comm | deptno |
+-------+--------+----------+------+------------+---------+--------+--------+
| 4
| akshay | clerk
| 100 | 1999-06-06 | 30000 | 40000 | 1 |
| 56 | aman | ceo
| 1000 | 1995-09-05 | 5000000 | 400000 | 2 |
| 57 | aayush | manager | 100 | 1999-09-06 | 50000 | 400000 | 4 |
| 123 | baba | salesman | 1 | 1998-06-04 | 3000 | 400 | 3 |
+-------+--------+----------+------+------------+---------+--------+--------+
mysql> create table nation(ncode integer(4) primary key,name varchar(20),capital
varchar(20),population integer(12),area integer(12));
Query OK, 0 rows affected (0.56 sec)
mysql> create table invention(invcode integer(6) primary key,ncode integer(4),ye
ar integer(4),inventor_name varchar(20),invention varchar(20), sex varchar(1));
Query OK, 0 rows affected (1.10 sec)
mysql> insert into invention values(1,2,1802,'edison','bulb','m');
Query OK, 1 row affected (0.10 sec)
mysql> insert into invention values(2,2,1860,'edison','phonograph','m');
Query OK, 1 row affected (0.07 sec)

mysql> insert into invention values(3,45,1973,'general motors','air bags','m');


Query OK, 1 row affected (0.08 sec)
mysql> insert into invention values(4,3,1910,'martin hall','foil','m');
Query OK, 1 row affected (0.07 sec)
mysql> insert into invention values(5,0,0001,'early man','wheel','m');
Query OK, 1 row affected (0.06 sec)
mysql> insert into invention values(6,2,1840,'aman','phone','m');
Query OK, 1 row affected (0.07 sec)
mysql> insert into nation values(2,'USA','washington DC',8100000,123456789);
Query OK, 1 row affected (0.15 sec)
mysql> insert into nation values(0,'Gondwana','no',1234,987654321);
Query OK, 1 row affected (0.14 sec)
mysql> insert into nation values(3,'UK','londoan',5688888,9876531);
Query OK, 1 row affected (0.10 sec)
mysql> insert into nation values(45,'Russia','moscow',91222223,987653231);
Query OK, 1 row affected (0.08 sec)
mysql> select*from nation;

OUTPUT
+-------+----------+---------------+------------+-----------+
| ncode | name | capital
| population |
area
|
+-------+----------+---------------+------------+-----------+
| 0 | Gondwana | no
|
1234 | 987654321 |
| 2 | USA
| washington DC | 8100000 | 123456789 |
| 3 | UK
| londoan
| 5688888 | 9876531 |
| 45 | Russia
| moscow
| 91222223 | 987653231 |
+-------+----------+---------------+------------+-----------+

mysql> select ename,hiredate from emp;

OUTPUT
+--------+------------+
| ename | hiredate |
+--------+------------+
| akshay | 1999-06-06 |
| aman | 1995-09-05 |
| aayush | 1999-09-06 |
| baba | 1998-06-04 |
+--------+------------+
mysql> select ename as EMPLOYEES,sal from emp
-> where sal >20000;

OUTPUT
+-----------+---------+
| EMPLOYEES | sal |
+-----------+---------+
| akshay | 30000 |
| aman | 5000000 |
| aayush | 50000 |
+-----------+---------+
3 rows in set (0.00 sec)
mysql> select ename,job from emp
-> where not(job='clerk' or 'salesman');

OUTPUT
+--------+----------+
| ename | job |
+--------+----------+
| aman | ceo |
| aayush | manager |
| baba | salesman |
+--------+----------+
3 rows in set, 1 warning (0.04 sec)

mysql> select ename,job from emp


-> where job NOT in ('clerk','salesman');

OUTPUT
+--------+---------+
| ename | job |
+--------+---------+
| aman | ceo |
| aayush | manager |
+--------+---------+
2 rows in set (0.06 sec)
mysql> select ename,empno from emp
-> order by ename;

OUTPUT
+--------+-------+
| ename | empno |
+--------+-------+
| aayush | 57 |
| akshay | 4 |
| aman | 56 |
| baba | 123 |
+--------+-------+
4 rows in set (0.06 sec)
mysql> select ename from emp
-> where ename like '%s%';

OUTPUT
+--------+
| ename |
+--------+
| akshay |
| aayush |
+--------+
2 rows in set (0.00 sec)
mysql> select ename,sal,comm from emp
-> where sal < comm;

OUTPUT
+--------+-------+--------+
| ename | sal | comm |
+--------+-------+--------+
| akshay | 30000 | 40000 |
| aayush | 50000 | 400000 |
+--------+-------+--------+
mysql> select ename,job from emp
-> where deptno in(2,4);

OUTPUT
+--------+---------+
| ename | job |
+--------+---------+
| aman | ceo |
| aayush | manager |
+--------+---------+
2 rows in set (0.00 sec)
mysql> select inventor_name,invention,year from invention
-> where inventor_name='edison';

OUTPUT
+---------------+------------+------+
| inventor_name | invention | year |
+---------------+------------+------+
| edison
| bulb
| 1802 |
| edison
| phonograph | 1860 |
+---------------+------------+------+
2 rows in set (0.00 sec)
mysql> select name,population from nation
-> where population >2300000 and population < 8200000
-> order by name;

OUTPUT
+------+------------+
| name | population |
+------+------------+
| UK | 5688888 |
| USA | 8100000 |
+------+------------+
2 rows in set (0.00 sec)

mysql> select*from invention


-> where year not between 1800 and 1850
-> order by invention;

OUTPUT
+---------+-------+------+----------------+------------+------+
| invcode | ncode | year | inventor_name | invention | sex |
+---------+-------+------+----------------+------------+------+
|
3 | 45 | 1973 | general motors | air bags | m |
|
4 | 3 | 1910 | martin hall | foil
|m |
|
2 | 2 | 1860 | edison
| phonograph | m |
|
5 | 0 | 1 | early man | wheel | m |
+---------+-------+------+----------------+------------+------+

mysql> select lcase(invention) from invention;

OUTPUT
+------------------+
| lcase(invention) |
+------------------+
| bulb
|
| phonograph
|
| air bags
|
| foil
|
| wheel
|
| phone
|
+------------------+
6 rows in set (0.12 sec)
mysql> select min(year) as earliest,max(year) as latest from invention;

OUTPUT
+----------+--------+
| earliest | latest |
+----------+--------+
|
1 | 1973 |
+----------+--------+
1 row in set (0.03 sec)
mysql> select name,count(inventor_name) from nation,invention
-> where nation.ncode=invention.ncode
-> group by name;

OUTPUT
+----------+----------------------+
| name | count(inventor_name) |
+----------+----------------------+
| Gondwana |
1|
| Russia |
1|
| UK
|
1|
| USA |
3|
+----------+----------------------+
mysql> insert into emp values(321,'sharma','head',4,'2013-9-3',1999000,NULL,2);
Query OK, 1 row affected (0.37 sec)
mysql> update emp
-> set comm=1000 where ename='sharma';
Query OK, 1 row affected (0.15 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select*from emp;

OUTPUT
+-------+--------+----------+------+------------+---------+--------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+-------+--------+----------+------+------------+---------+--------+--------+
| 4 | akshay | clerk | 100 | 1999-06-06 | 30000 | 40000 | 1 |
| 56 | aman | ceo | 1000 | 1995-09-05 | 5000000 | 400000 | 2 |
| 57 | aayush | manager | 100 | 1999-09-06 | 50000 | 400000 | 4 |
| 123 | baba | salesman | 1 | 1998-06-04 | 3000 | 400 | 3 |
| 321 | sharma | head | 4 | 2013-09-03 | 1999000 | 1000 | 2 |
+-------+--------+----------+------+------------+---------+--------+--------+
5 rows in set (0.00 sec)

mysql> delete from emp


-> where empno=321;
Query OK, 1 row affected (0.11 sec)
mysql> create table emptest like emp;
Query OK, 0 rows affected (0.67 sec)
mysql> insert into emptest select*from emp;
Query OK, 4 rows affected (0.10 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> alter table emptest
-> add sex varchar(1);
Query OK, 0 rows affected (4.36 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> update emptest
-> set sal= sal+500 where deptno=2;
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select*from emptest;

OUTPUT
+-------+--------+----------+------+------------+---------+--------+--------+-----+
| empno | ename | job | mgr | hiredate | sal | comm | deptno | sex |
+-------+--------+----------+------+------------+---------+--------+--------+------+
| 4 | akshay | clerk | 100 | 1999-06-06 | 30000 | 40000 | 1 | NULL |
| 56 | aman | ceo | 1000 | 1995-09-05 | 5000500 | 400000 | 2 | NULL |
| 57 | aayush | manager | 100 | 1999-09-06 | 50000 | 400000 | 4 | NULL |
| 123 | baba | salesman | 1 | 1998-06-04 | 3000 | 400 | 3 | NULL |
+-------+--------+----------+------+------------+---------+--------+--------+------+
4 rows in set (0.00 sec)

mysql> alter table emptest


-> modify sex varchar(6);
Query OK, 4 rows affected (1.60 sec)
Records: 4 Duplicates: 0 Warnings: 0

mysql> describe emptest;

OUTPUT
+----------+-------------+------+-----+---------+-------+
| Field | Type
| Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| empno | int(4) | NO | PRI | NULL |
|
| ename | varchar(25) | YES | | NULL |
|
| job | varchar(10) | YES | | NULL |
|
| mgr | int(4) | YES | | NULL |
|
| hiredate | date
| YES | | NULL |
|
| sal | int(7) | YES | | NULL |
|
| comm | int(7) | YES | | NULL |
|
| deptno | int(2) | YES | | NULL |
|
| sex | varchar(6) | YES | | NULL |
|
+----------+-------------+------+-----+---------+-------+
9 rows in set (0.13 sec)
mysql> drop table emptest;
Query OK, 0 rows affected (0.45 sec)

You might also like