0% found this document useful (0 votes)
0 views

querys new

The document contains SQL commands for creating and manipulating employee and department tables, including inserting data, querying for maximum and minimum salaries, and performing joins. It also includes commands for creating and querying sailor and boat tables, demonstrating various SQL functions like TRIM, CONCAT, and string comparison. Additionally, it showcases how to find specific data such as reserved boats and sailor names based on certain conditions.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

querys new

The document contains SQL commands for creating and manipulating employee and department tables, including inserting data, querying for maximum and minimum salaries, and performing joins. It also includes commands for creating and querying sailor and boat tables, demonstrating various SQL functions like TRIM, CONCAT, and string comparison. Additionally, it showcases how to find specific data such as reserved boats and sailor names based on certain conditions.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

create table EMP(id integer,sal integer,dno integer);

insert into EMP values(1, 2000,10);


insert into EMP values(2, 1100,20);
insert into EMP values(3, 2500,10);
insert into EMP values(4, 3000,30);
insert into EMP values(5, 2000,20);

select max(sal) from EMP;


max(sal)
3000

select min(sal) from EMP;


min(sal)
1100
select max(sal) from EMP where sal<(select max(sal) from EMP);
max(sal)
2500

select min(sal) as 'second minimum sal' from EMP where sal > (select min(sal) from EMP);
second minimum sal
2000

select dno,count( dno),max(sal) from EMP t group by dno having 1 < count(t.dno) ;
dno count( dno) max(sal)
10 2 2500
20 2 2000

select dno, max(sal) from EMP group by dno having count(dno)<2;


dno max(sal)
30 3000

select max(sal) ‘second max sal’ from EMP where sal not in (select max(sal) from EMP);
second max sal’
2500

select sum(sal) from EMP;


sum(sal)
10600
select sum(sal)/count(sal) as average from EMP;
average
2120.0000
select avg(sal) from EMP;
avg(sal)
2120.0000
1.create table dept(dno integer,dname text,loc varchar(40));
-- Display the structure of a table dept
desc dept;

Field Type Null Key Default Extra


dno int YES NULL
dname text YES NULL
loc varchar(40) YES NULL

2.create table EMP(eno integer,ename char(30),sal integer,dno integer);


desc EMP;

Field Type Null Key Default Extra


eno int YES NULL
ename char(30)YES NULL
sal int YES NULL
dno int YES NULL

// adding primary key to dept table;


alter table dept add primary key (dno);
desc dept;

Field Type Null Key Default Extra


dno int NO PRI NULL
dname text YES NULL
loc varchar(40) YES NULL

alter table EMP add primary key (eno);


desc EMP;

Field Type Null Key Default Extra


eno int NO PRI NULL
ename char(30)YES NULL
sal int YES NULL
dno int YES NULL

// adding Foreign key to emp table;


alter table EMP add Foreign key (dno) references dept(dno);
desc EMP;

Field Type Null Key Default Extra


eno int NO PRI NULL
ename char(30)YES NULL
sal int YES NULL
dno int YES MUL NULL

//inserting data into dept table;

insert into dept values (10,'Accounting','New York');

insert into dept values (20,'Research','Dallas');

insert into dept values (30,'Sales','Chicago');


insert into dept values (40,'Operations','Boston');

dno dname loc


10 Accounting New York
20 Research Dallas
30 Sales Chicago
40 Operations Boston

// adding column to emp table


alter table EMP add column job varchar(19);
desc EMP;

Field Type Null Key Default Extra


eno int NO PRI NULL
ename char(30) YES NULL
sal int YES NULL
dno int YES MUL NULL
job varchar(19) YES NULL

insert into EMP (eno,ename,job,sal,dno) values (7369,'SMITH','CLERK',


'800.00', '20');

insert into EMP (eno,ename,job,sal,dno) VALUES ('7499','ALLEN','SALESMAN',


'1600.00','30');

select * from EMP;

eno ename sal dno job


7369 SMITH 800 20 CLERK
7499 ALLEN 1600 30 SALESMAN
7521 WARD 1250 30 SALESMAN
7566 JONES 2975 20 MANAGER

select eno,ename,dname,sal from EMP,dept where EMP.dno=dept.dno;

eno ename dname sal


7782 CLARK Accounting 2450
7839 KING Accounting 5000
7934 MILLER Accounting 1300
7369 SMITH Research 800
7566 JONES Research 2975

select eno,ename,sal from EMP order by dno


eno ename sal
7782 CLARK 2450
7839 KING 5000
7934 MILLER 1300
7369 SMITH 800
7566 JONES 2975
select eno,ename,sal from EMP order by dno desc
eno ename sal
7369 SMITH 800
7566 JONES 2975
7782 CLARK 2450
7839 KING 5000
7934 MILLER 1300

select EMP.eno,dept.dname from EMP left JOIN dept ON EMP.dno = dept.dno;

eno dname
7369 Research
7499 Sales
7521 Sales
7566 Research
7654 Sales

select EMP.eno, dept.dname from EMP right JOIN dept ON EMP.dno = dept.dno;
eno dname
7782 Accounting
7839 Accounting
7934 Accounting
7369 Research
7566 Research
7788 Research
7876 Research
7902 Research
7499 Sales
7521 Sales
7654 Sales
7698 Sales
7844 Sales
7900 Sales
NULL Operations
select EMP.eno,dept.dname from EMP cross JOIN dept -- ON EMP.dno = dept.dno;

eno dname
7369 Operations
7369 Sales
7698 Research
7698 Accounting
7782 Operations
7782 Sales
7876 Research
7876 Accounting

select EMP.eno,dept.dname from EMP cross JOIN dept ON EMP.dno = dept.dno;


eno dname
7782 Accounting
7839 Accounting
7934 Accounting
7521 Sales
7654 Sales
7698 Sales
7844 Sales
7900 Sales
select EMP.eno,dept.dname from EMP cross JOIN dept where EMP.dno = dept.dno;
eno dname
7782 Accounting
7839 Accounting
7934 Accounting
7369 Research
7566 Research
7788 Research
7876 Research
7902 Research
7499 Sales
7521 Sales
7654 Sales
7698 Sales
7844 Sales
7900 Sales
select A.eno,b.ename from EMP A,EMP b where A.dno<>b.dno;
eno ename
7934 SMITH
7900 SMITH
7844 SMITH

select dno, count(distinct dno) ,job, count(job) from EMP group by dno,job

dno count(dno) job count(job)


10 1 CLERK 1
10 1 MANAGER 1
10 1 PRESIDENT 1

create table EMP(id integer,date Date);


insert into EMP values(1, "1972-07-22");
-- SELECT *, DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),date))+%Y)+0 AS age
-- FROM EMP;
-- SELECT *, DATE_FORMAT(FROM_DAYS(DATEDIFF(CURDATE(), date)), '%Y') +0 AS age
-- FROM EMP;
select Date_Format(date,'%W %D %M %Y') from EMP
Date_Format(date,'%W %D %M %Y')
Saturday 22nd July 1972

create table Sailors (sid Integer primary key , sname Text, rating Integer, age Integer);
desc Sailors;

Field Type Null Key Default Extra


sid int NO PRI NULL
sname text YES NULL
rating int YES NULL
age int YES NULL

create table Boats (bid Integer primary key, bname Text, color Text);
desc Boats;
Field Type Null Key Default Extra
bid int NO PRI NULL
bname text YES NULL
color text YES NULL
create table Reserves (sid Integer, bid integer, day Date,FOREIGN KEY (sid) REFERENCES
Sailors(sid),FOREIGN KEY (bid) REFERENCES Boats(bid));
desc Reserves;

Field Type Null Key Default Extra


sid int YES MUL NULL
bid int YES MUL NULL
day date YES NULL

insert into Sailors values(101,'lubber',5,30),(102,'ramu',4,27),(103,'laxman',4,28),(104,'raju',3,29);

select * from Sailors;

sid sname rating age


101 lubber 5 30
102 ramu 4 27
103 laxman 4 28
104 raju 3 29
insert into Boats values (1,'vag','red'),(2,'vce','green'),(3,'vpc','black'),(4,'vcp','yellow');
select * from Boats;
bid bname color
1 vag red
2 vce green
3 vpc black
4 vcp yellow
insert into Reserves values (101,1,'2006-12-17') ,(101,1,'2010-10-05'),(102,1,'2006-12-17'),(101,2,'2006-
12-17'),
(101,3,'2016-12-17'),(103,3,'2006-12-17'),(101,4,'2006-12-17'),(104,1,'2006-12-17'),(101,2,'2006-12-17');

select * from Reserves;


sid bid day
101 1 2006-12-17
101 1 2010-10-05
102 1 2006-12-17
101 2 2006-12-17
101 3 2016-12-17
103 3 2006-12-17
101 4 2006-12-17
104 1 2006-12-17
101 2 2006-12-17
select * from Boats ;
bid bname color
1 vag red
2 vce green
3 vpc black
4 vcp yellow
select * from Reserves;
sid bid day
101 1 2006-12-17
101 1 2010-10-05
102 1 2006-12-17
101 2 2006-12-17
101 3 2016-12-17
103 3 2006-12-17
101 4 2006-12-17
104 1 2006-12-17
102 2 2006-12-17

Consider the SAILOR DATABASE


Sailors (sid:string, sname:string, rating:integer, age:real)
Boats (bid Integer, bname Text, color Text)
Reserves (sid integer, bid integer, day Date)
Based on the above schemas answer the following queries.
Based on the above schema, write the corresponding SQL

queries for the following:


(i) Find the colors of boats reserved by ‘Lubber’.

select distinct b.color from Boats b,Reserves r,Sailors s where b.bid=r.bid and r.sid=s.sid and
sname='lubber'

color
red
green
black
yellow

(ii) Find the names of sailors who have reserved at least one
Boat
select distinct sname from Sailors s, Reserves r where r.sid=s.sid;
sname
lubber
ramu
laxman

(iii) Find the names of sailors who have reserved a red or green
Boat

SELECT R.sid FROM Boats B, Reserves R WHERE R.bid = B.bid AND B.color = ‘red’
UNION
SELECT R2.sid FROM Boats B2, Reserves R2 WHERE R2.bid = B2.bid AND B2.color = ‘green’

(iv) Find the names of the sailors who have reserved both a
Red boat and a Green boat.

SELECT R.sid FROM Boats B, Reserves R WHERE R.bid = B.bid AND B.color = ‘red’ INTERSECT
SELECT R2.sid FROM Boats B2, Reserves R2 WHERE R2.bid = B2.bid AND B2.color = ‘green’

(v) Find names of sailors who have reserved all boats

SELECT S.sname FROM Sailors S WHERE NOT EXISTS ( ( SELECT B.bid FROM Boats B) EXCEPT
( SELECT R.bid FROM Reserves R WHERE R.sid = S.sid ) )
1.select round((sin(30)),0);
2.The TRIM() function is a powerful tool used to remove unwanted characters from the beginning, end,
or both ends of a string. This function is particularly useful in data cleansing tasks, ensuring that strings
are free from leading and trailing whitespace or specified characters.
Key Uses of the TRIM() Function:
One of the most common applications is to clean up user input by removing excess spaces.
TRIM() can strip unwanted prefixes or suffixes from data fields, aiding in maintaining consistent and
clean data.
Before storing or displaying data, TRIM() helps in standardizing strings to remove any unnecessary
padding.
Syntax:
TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM ] str)
Arguments:
Name Description
BOTH Removes characters from both the left and right sides of the string.
LEADING Removes characters only from the left side of the string.
TRAILING Removes characters only from the right side of the string.
remstr The string to be removed from the specified side(s) of the original string.
str The actual string from which characters are to be removed.

select trim(Trailing 'M' from 'MadaM');


select trim(Leading 'M' from 'MadaM');
select trim(both 'M' from 'MadaM');
select trim( 'M' from 'MadaM');
 RTRIM(): Removes trailing spaces or specified characters from the end of a string.
 LTRIM(): Removes leading spaces or specified characters from the beginning of a string.
select Rtrim('MadaM ');
select Ltrim(' MadaM ');
The CONCAT() function is used for string concatenation. It allows you to combine two or more strings
into a single string. This function is especially useful for data formatting, combining fields, and creating
readable outputs from data.
select concat("Vaagdevi","Engineering","college");
LPAD() left pads a string with another string. The actual string, a number indicating the length of the
padding in characters (optional) and the string to be used for left padding - all are passed as arguments.
select lpad('vaagdevi',6,'@');
select lpad('vaagdevi',10,'@');
RPAD() function pads strings from right. The actual string which is to be padded, the length of the string
returned after padding and string which is used for padding - all these are passed as arguments.
select rpad('vaagdevi',6,'@');
select rpad('vaagdevi',10,'@');
strcmp() function is used to compare two strings. It returns 0 if both of the strings are same and returns -1
when the first argument is smaller than the second according to the defined order and 1 when the second
one is smaller the first one.
SELECT STRCMP('mytesttext', 'mytesttext');
SELECT STRCMP('mytesttext', 'mytest_text');
SUBSTR() returns the specified number of characters from a particular position of a given string.
SUBSTR() is a synonym for SUBSTRING().
SELECT SUBSTR('computer science Engineering',4,3);
POSITION() returns the position of a substring within a string
SELECT POSITION("er" IN "Vaagdevi ENgineering");
select sysDate();
Select now();
select curdate();
trim(Trailing 'M' from 'MadaM')
Mada
trim(Leading 'M' from 'MadaM')
adaM
trim(both 'M' from 'MadaM')
ada
trim( 'M' from 'MadaM')
ada
Rtrim('MadaM ')
MadaM
Ltrim(' MadaM ')
MadaM
concat("Vaagdevi","Engineering","college")
VaagdeviEngineeringcollege
lpad('vaagdevi',6,'@')
vaagde
lpad('vaagdevi',10,'@')
@@vaagdevi
rpad('vaagdevi',6,'@')
vaagde
rpad('vaagdevi',10,'@')
vaagdevi@@
STRCMP('mytesttext', 'mytesttext')
0
STRCMP('mytesttext', 'mytest_text')
1
SUBSTR('computer science Engineering',4,3)
put
POSITION("er" IN "Vaagdevi ENgineering")
16
sysDate()
2025-02-22 10:15:48
now()
2025-02-22 10:15:48
curdate()
2025-02-22

You might also like