Professional Documents
Culture Documents
There is a major difference between a database and a table. The differences are
as follows:
Tables are a way to represent the division of data in a database while the
database is a collection of tables and data.
Tables are used to group the data in relation to each other and create a
dataset. This dataset will be used in the database. The data stored in the table in
any form is a part of the database, but the reverse is not true.
A database is a collection of organized data and features used to access them,
whereas the table is a collection of rows and columns used to store the data.
Or
SELECT MAX(salary)
FROM employees
WHERE salary NOT IN ( SELECT Max(salary) FROM employees);
23>> Can you tell what are the different set operations available in MySQL?
inner join,left join,right join,cross join , self join
24>> What are the differences between a primary key and a foreign key?
The database table uses a primary key to identify each row uniquely. It is
necessary to declare the primary key on those tables that require to create a
relationship among them. One or more fields of a table can be declared as the
primary key.
When the primary key of any table is used in another table as the primary key
or another field for making a database relation, then it is called a foreign key.
25>> Which MySQL function is used to concatenate string?
CONCAT() function is used to combine two or more string data.
26>> What is the difference between the Primary key and the Unique key?
Unique data is stored in the primary key and unique key fields. The primary key
field never accepts NULL value but a unique key field accepts a NULL value.
27>> How can you calculate the sum of any column of a table?
SUM() function is used to calculate the sum of any column.
28>> How can you count the total number of records of any table?
COUNT() function is used to count the total number of records of any table.
Syntax:
COMMIT;
Syntax:
ROLLBACK;
Create Table
Insert Records
SELECT id, name FROM personal WHERE city < > "Agra";
AND, OR, NOT
SELECT * FROM personal WHERE age >= 18 AND age <= 21;
SELECT * FROM personal WHERE (city = "Bhopal" OR city = "Agra") AND gender = "M";
IN & NOT IN
SELECT * FROM personal WHERE age IN(18,21);
SELECT * FROM personal WHERE name LIKE "r%" OR name LIKE "s%";
DISTINCT
MIN
SUM
AVG
SELECT AVG(percentage) AS Average FROM personal;
UPDATE
UPDATE personal
SET phone = "4055555"
WHERE id = 1;
UPDATE personal SET percentage = 66 WHERE id = 2;
UPDATE personal SET age = 18 WHERE id IN (2,3);
UPDATE personal SET age = 19;
COMMIT & ROLLBACK
Example-1
SELECT * FROM personal;
COMMIT;
Example-2
SELECT * FROM personal;
COMMIT;
COMMIT;
ROLLBACK;
DELETE
//COMMIT;
//ROLLBACK;
PRIMARY KEY & FOREIGN KEY
create table "city"
SELECT p.id,p.name,p.percentage,p.age,p.gender,c.cityname
FROM personal p INNER JOIN city c
ON p.city = c.cid;
SELECT p.id,p.name,p.percentage,p.age,p.gender,c.cityname
FROM personal p INNER JOIN city c
ON p.city = c.cid
WHERE c.cityname = "Agra";
SELECT p.id,p.name,p.percentage,p.age,p.gender,c.cityname
FROM personal p INNER JOIN city c
ON p.city = c.cid
WHERE c.cityname = "Agra"
ORDER BY p.name;
SELECT p.id,p.name,p.percentage,p.age,p.gender,c.cityname
FROM personal p JOIN city c
ON p.city = c.cid
WHERE c.cityname = "Agra"
ORDER BY p.name;
LEFT JOIN
SELECT p.id,p.name,p.percentage,p.age,p.gender,c.cityname
FROM personal p LEFT JOIN city c
ON p.city = c.cid;
SELECT p.id,p.name,p.percentage,p.age,p.gender,c.cityname
FROM personal p LEFT JOIN city c
ON p.city = c.cid
WHERE gender = "M";
SELECT p.id,p.name,p.percentage,p.age,p.gender,c.cityname
FROM personal p LEFT JOIN city c
ON p.city = c.cid
WHERE gender = "M"
ORDER BY name;
RIGHT JOIN
SELECT p.id,p.name,p.percentage,p.age,p.gender,c.cityname
FROM personal p RIGHT JOIN city c
ON p.city = c.cid;
CROSS JOIN
SELECT
p.id,p.name,p.percentage,p.age,p.gender,c.cityname,cr.course
_name
FROM personal p INNER JOIN city c
ON p.city = c.cid
INNER JOIN courses cr
ON p.courses = cr.course_id;
SELECT
p.id,p.name,p.percentage,p.age,p.gender,c.cityname,cr.course_name
FROM personal p INNER JOIN city c
ON p.city = c.cid
INNER JOIN courses cr
ON p.courses = cr.course_id
WHERE c.cityname = "Agra";
SELECT city,COUNT(city)
FROM personal
GROUP BY city;
SELECT c.cityname,COUNT(p.city)
FROM personal p INNER JOIN city c
ON p.city = c.cid
GROUP BY city;
SELECT c.cityname,COUNT(p.city) AS Total
FROM personal p INNER JOIN city c
ON p.city = c.cid
GROUP BY city;
SELECT CURDATE();
SELECT SYSDATE();
SELECT NOW();
# Q2. Write a MySQ1 query to find the players from the test match 1
having popularity higher than the average popularity.
select player_name, Popularity from cricket_1 WHERE Popularity>
(SELECT AVG(Popularity) FROM cricket_1);
#Q3. Find player_id and player name that are common in the test
match 1 and test match 2.
SELECT player_id, player_name FROM cricket_1
WHERE cricket_1.player_id IN (SELECT player_id FROM cricket_2);
#04. Retrieve player id, runs, and player name from cricket 1 table
and display list of the players where the runs are more than the
average runs.
SELECT player_id, runs, player name FROM cricket 1 WHERE
runs>(SELECT AVG(runs) FROM cricket_1);
#Q6. Write a query to extract all the columns from cricket 1 where
player_name starts with 'y' and ends with 'v'.
SELECT FROM cricket 1 WHERE player name LIKE 'y%v";
# Q7. Write a query to extract all the columns from cricket_1 where
player_name does not end with 't'.
SELECT FROM cricket_1 WHERE player_name NOT LIKE '%t';
Q11. Extract the Player_Id and Player name of the players where the
charisma value is null.
SELECT Player_Id, Player_Name FROM new cricket WHERE Charisma
IS NULL;
#Q12. Separate all Player Id into single numeric ids (example PLI - 1).
SELECT Player_Id, SUBSTR(Player Id, 3)
FROM new cricket;
#Q13. Write a MySQL query to extract Player_Id, Player Name and
charisma where the charisma is greater than 25. SELECT Player Id,
Player Name, charisma FROM new_cricket WHERE charisma > 25;