MySQL classic interview questions

Database optimization:
This optimization rule can be summarized into 5 levels:
1. Reduce data access (reduce disk access)
2. Return less data (reduce network transfer or disk access)
3. Reduce the number of interactions (reduce network transfer)
4. Reduce server CPU overhead (reduce CPU and memory overhead)
5. Utilize more resources (increase resources)

What fields do we generally build indexes on?
This is a very complex topic, which requires a full analysis of the business and data to get results. The primary key and foreign key usually have an index, and other fields that need to be indexed should meet the following conditions:
1. The field appears in the query condition, and the query condition can use the index;
2. The statement execution frequency is high, thousands of times a day The above;
3. The set of records that can be filtered by field conditions is very small. What is the appropriate proportion of data filtering?
There is no fixed value, and it needs to be evaluated according to the amount of table data. The following is an empirical formula that can be used for quick evaluation:
small table (table with less than 10,000 records): screening ratio < 10%;
large table: (screening returns the number of records) <(Total number of records in the table*Length of a single record)/10000/
16The length of a single record≈the sum of the average content length of the fields + the number of fields*2

 

-- current time
select CURTIME();
-- current date
select NOW();
-- query data of the current day
select * from table name where TO_DAYS(time field)=TO_DAYS(NOW());
-- query data of this week
SELECT * FROM table name WHERE YEARWE--EK(date_format(time field,'%Y-%m-%d')) = YEARWEEK(now());
--
SELECT * FROM table name where date_sub(curdate( ), INTERVAL 7 DAY) <= date (time field);
-- query data of this month
select * from table name where DATE_FORMAT(time field,'%Y%m') =DATE_FORMAT(CURDATE(),'%Y% m');
-- last month
SELECT * FROM table name WHERE PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( time field name, '%Y%m' ) ) =1

 

/*-----------------------------------------The first major topic--- ---------------------------------------------------------*/

S (SO, SN, SD, SA) Student ID, student name, affiliation, student age

C(CO,CN) course number, course name

SC_1(SO,CO,G) student number, course number taken, academic record

 

/*1 Query the elective course named 'tax-based student number and name'*/
--Method 1: connect query

SELECT s.SO,s.SN FROM S s,C c,sc_1 sc
where c.CN='税收基础'
and s.so=sc.SO and sc.co=c.CO;

--Method 2: Nested query
SELECT so, sn from s
where so in(
SELECT so from sc_1 where co in (
SELECT co from c where cn='tax basis'
)
)

/*2 Query the elective course number as 'c002' student name and affiliation'*/ --Method
1: connect query
SELECT s.SN,s.SD from ss,cc,sc_1 sc
where c.co='c002'
and c.co=sc.CO and s.so=sc.so;

--Method 2: Nested query
SELECT sn,sd from s
where so in(
SELECT so from sc_1 where co IN (
SELECT co from c
where co='c002'
)
)

/*3 Query the names and affiliations of students whose course number is 'c005'*/
SELECT sn,sd from s
where so not IN
(select so from sc_1 WHERE co='C005');

/*4 Query the names and affiliations of all the students taking all the elective courses'*/
SELECT sn,sd from s
where so in
(
SELECT so from sc_1
GROUP BY so
HAVING COUNT(co)=(SELECT COUNT(co) FROM c)
);

/*5 Query the number of students who have taken the course*/
SELECT COUNT(DISTINCT so) as the number of elective courses from sc_1;

/*6 Check the names and affiliations of students with more than 5 new elective courses*/
SELECT sn,sd from s
where so IN
(SELECT so from sc_1
GROUP BY so
HAVING COUNT(so)>5
);

 

/*-----------------------------------------Second question--- ---------------------------------------------------------*/

S(SNO, SNAME) student relationship, SNO student number, SNAME name

C(CNO, CHAME, CTEACHER) course relationship, CNO course number, CHAME course name, teacher

SC (SNO, CNO, SCGRADE) course selection relationship, SCGRADE score

 

/*The first question finds all students who have not taken Mr. Li Ming's courses*/
SELECT s.sname FROM student s
where sno not IN
(SELECT DISTINCT(sc1.cno) from class c,sc sc1,student s
where c .cno=sc1.cno and sc1.sno=s.sno
and c.cteacher='Li Ming'
);


/*The second question lists the names and average grades of students who have more than two failed courses*/
select s.sname as student name,avg(ssc.scgrade) as average grade from student s
,sc ssc,(select sno from sc
where scgrade<60
GROUP BY sno
HAVING COUNT((cno)>=2)) a
where s.sno=a.sno ​​and ssc.sno=a.sno
​​GROUP BY s.sno,s.sname;


/*The third question lists the names of students who have studied both course 1 and course 2*/
Select s.sno,s.sname FROM student s
where s.sno in
(
Select sc2.sno FROM sc sc2,class c2 Where sc2.cno=c2.cno AND c2.cno IN('1','2')
GROUP BY sc2.sno
HAVING COUNT(DISTINCT c2.cno)=2
);

/*The fourth question lists all the student numbers of students whose grades in class 1 are higher than those in class 2*/
SELECT sc1.sno as student number from sc sc1,sc sc2 where
sc1.cno='1'
and sc2. cno='2'
and sc1.sno=sc2.sno
and sc1.scgrade>sc2.scgrade;

/*The fifth lists all the student numbers of students whose grades in class 1 are higher than those of class 2, and the grades of classes 1 and 2*/

SELECT sc1.sno as student number,sc1.scgrade as Chinese grade,sc2.scgrade as math grade from sc sc1,sc sc2 where
sc1.cno='1'
and sc2.cno='2'
and sc1.sno=sc2. sno
and sc1.scgrade>sc2.scgrade

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324723963&siteId=291194637