Database SQL Query
Database SQL Query
Database SQL Query
2)
);
create table employee
(
eID int not null,
ename varchar(15),
street varchar(10),
city varchar(10),
age int,
primary key(eID)
);
The UNIQUE constraint ensures that all values in a column are different.
Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for
uniqueness for a column or set of columns.
The "PersonID" column in the "Persons" table is the PRIMARY KEY in the
"Persons" table.
The "PersonID" column in the "Orders" table is a FOREIGN KEY in the "Orders"
table.
3)
4)
Show databases;
5)
6)
SELECT *
FROM student_details
LIMIT 4;
SELECT *
FROM student_details
LIMIT 2,5;[1st two numbers omit, next 5 numbers display]
9)
SELECT Name
FROM student_details
ORDER BY Name;
SELECT Name, Age, City
FROM student_details
ORDER BY Name; [default ASC]
SELECT Roll, Age, City
FROM student_details
ORDER BY Roll DESC;
SELECT 2+5;
SELECT 5-2;
SELECT 5*2;
SELECT 10/3;
SELECT 10%3;
11)
SELECT City
FROM student_details
WHERE Gender='Female';
SELECT *
FROM student_details
WHERE Age = 18;
SELECT *
FROM `student_details`
WHERE Age<=23;
SELECT *
FROM student_details
WHERE Age>=25;
SELECT *
FROM student_details
WHERE Roll = 105;
SELECT *
FROM student_details
WHERE Gender='Male' OR GPA>=3.5;
SELECT *
FROM student_details
WHERE Gender='Male' AND GPA>=3.5;
SELECT *
FROM student_details
WHERE Country='Dhaka'
AND
(Gender='Male' OR GPA >= 3.5);
12)
SELECT *
FROM student_details
WHERE Country IN ('Mymensingh','Sylet','Barisal');
[we can use ‘IN’ instead of ‘OR’]
SELECT *
FROM student_details
WHERE Country NOT IN ('Mymensing','Sylet','Barisal');
13)
SELECT *
FROM student_details
WHERE Name LIKE 's%'; [Name start with ‘s’ character]
SELECT *
FROM student_details
WHERE Name LIKE '%a' [Name last character will be ‘a’]
SELECT *
FROM student_details
WHERE Name LIKE '%ai%';[‘ai’ character will be in name in any position]
SELECT *
FROM student_details
WHERE Name LIKE '%hi%';
SELECT *
FROM student_details
WHERE Name LIKE 'a__%';
[Finds any values that start with "a" and are at least 2 characters in
length]
SELECT *
FROM student_details
WHERE Name LIKE 'a___%';
[Finds any values that start with "a" and are at least 3 characters in
length]
SELECT *
FROM student_details
WHERE Name LIKE '%n_';
14)
15)
UPDATE teacher
SET Department='Textile'
WHERE ID = 4;
UPDATE teacher
SET Salary= Salary + 10000
WHERE Salary > 30000;
N.B: Be careful when updating records. If you omit the WHERE clause,
ALL records will be updated!
UPDATE Customers
SET ContactName='Juan';
16)
17)
SELECT UPPER(Country)
FROM student_details;
SELECT GREATEST(12,-5,100,56,99,45);
SELECT LEAST(12,-5,100,56,99,45);
SELECT POW(5,3);
SELECT LOG(3);
SELECT LOG10(3);
SELECT TRUNCATE(LOG(2),4);
SELECT RAND();
18)
DESCRIBE student_details;
DESCRIBE teacher;
19)
SELECT COUNT(*)
FROM student_details;
SELECT MAX(GPA)
FROM student_details;
SELECT MIN(GPA)
FROM student_details;
SELECT AVG(Price)
FROM Products;
20)
SELECT *
FROM teacher
WHERE Salary > (SELECT AVG(Salary) FROM teacher);
21)
Note: The HAVING clause was added to SQL because the WHERE keyword
cannot be used with aggregate functions.
23)
SQL INDEX:
SQL INJECTION:
SELECT UserId, Name, Password FROM Users WHERE UserId = 105 or 1=1;
24)
25)
ON std.Roll = exam.Roll
26)
ON std.Roll = exam.Roll;
27)
ON std.Roll = exam.Roll;
28)
ON std.Roll = exam.Roll;
SELF JOIN:
Study table(S_ID,C_ID,Since) here C_ID Primary key and S_ID Foreign
key
Select T1.S_ID
FROM Study as T1, Study as T2
Where T1.S_ID= T2.S_ID AND T1.C_ID <> T2.C_ID;
29)
SELECT Roll,Name,Gender
FROM student_details
UNION
SELECT Roll,Name,Gender
FROM exam; [Sequence must be same,Duplicate value will be removed]
30)
SELECT Roll,Name,Gender
FROM student_details
UNION ALL
SELECT Roll,Name,Gender
FROM exam;[Duplicate will not be removed]
select eID, ename, city
from employee;
31)
SELECT *
FROM student_view;
UPDATE student_view
SET Name= 'Fahim'
WHERE Roll=105;
32)
SELECT CURTIME();
SELECT NOW();
SELECT MAKEDATE(2020,365);
SELECT DAYNAME('2020-12-4');
SELECT MONTHNAME('2020-12-4');
select e.ename
from employee e, works w, company c
where (e.ename = w.ename and e.city = c.city and w.cname = c.cname);
Question-3: Find the names of all employees in the database who live in the same cities and
on the same streets as do their managers.
select p.ename
from employee p, employee r, manages m
where p.ename = m.ename and m.mname =r.ename and p.street = r.street and p.city = r.city;
result: empty;
a.
b.
c. Find the names of all employees who work for Pran.
Select *ename
from works
where cname = 'pran';