SQL Query Example and Sample SQL Commands Queries

MYSQL Query Example


CHAR_LENGTH() Example


CHAR_LENGTH() function is used to count the character in the given String.

  SELECT question, explainans, `view`, CHAR_LENGTH(explainans) AS lengt 
FROM `question` WHERE explainans IS NOT NULL AND CHAR_LENGTH(explainans) > 200 
ORDER BY `view` DESC



GROUP BY() Example

 SELECT created_at, COUNT(*) FROM `application_for_otherexam` WHERE exam_id = '8' 
GROUP BY DATE(created_at)

 SELECT created_at, COUNT(*) AS Total FROM `application_for_otherexam` 
WHERE exam_id = '8'  AND form_status='55' GROUP BY DATE(created_at)



General Sample MY SQL Query Example

 SELECT COUNT(*) AS Total FROM `application` WHERE formstatus IN('4', '44', '55') 
AND examid='8'

 SELECT school_name, code FROM `school_master` WHERE code IN('22','31','31')

 SELECT CONCAT(FIRST_NAME,' ',LAST_NAME) AS WORKER_NAME FROM WORKER WHERE 
CONCAT(FIRST_NAME,' ',LAST_NAME) LIKE '%hal'

 SELECT YEAR(dob), COUNT(*) AS TOTAL FROM STUDENT_DETAILS GROUP BY YEAR(DOB) 
HAVING TOTAL>10 ORDER BY YEAR(DOB) ASC

 SELECT CONCAT(FIRST_NAME, ' ', LAST_NAME), SALARY FROM WORKER WHERE SALARY
BETWEEN 50000 AND 100000 

 SELECT APPCOMMADDR1, LENGTH(APPCOMMADDR1) AS T1, APPCOMMADDR2, 
LENGTH(APPCOMMADDR2) AS T2, APPCOMMADDR3, LENGTH(APPCOMMADDR3) AS T3 
FROM `Data_Dump_Dept_exam_dec2019_20191203` HAVING T1>50 AND T2>50 AND T3>50


MYSQL Sub Queries Example

 SELECT udiscode FROM schoolmaster WHERE code IN ( SELECT SCHL FROM `udise_mapping`
GROUP BY SCHL HAVING COUNT(*)>1)



CONCAT(), UPPER(), LOWER() Function Example

 SELECT UPPER(FIRST_NAME) AS FNAME, UPPER(LAST_NAME) AS LNAME, 
CONCAT(UPPER(FIRST_NAME),' ',UPPER(LAST_NAME))AS FULLNAME FROM WORKER 
ORDER BY FULLNAME ASC

 SELECT CONCAT(FIRST_NAME,' ',LAST_NAME) AS WORKER_NAME FROM WORKER WHERE 
CONCAT(FIRST_NAME,' ',LAST_NAME) LIKE '%hal'



SQL DATE(), YEAR(), MONTH() Function Example

 SELECT CURDATE() 

 SELECT YEAR(CURDATE())

 SELECT MONTH(CURDATE())

 SELECT DAY(CURDATE())

 SELECT NOW()

 SELECT YEAR(NOW())

 SELECT MONTH(NOW())

 SELECT HOUR(NOW())

 SELECT MINUTE(NOW())

 SELECT SECOND(NOW())
 SELECT * FROM WORKER WHERE MONTH(JOINING_DATE) = 6 ORDER BY SALARY DESC LIMIT 2

 SELECT  MONTHNAME(STR_TO_DATE(MONTH(dob),'%m')) AS mname, COUNT(*) AS totalno 
FROM `student_details` WHERE MONTH(dob) =1 GROUP BY MONTHNAME(STR_TO_DATE(MONTH(dob),'%m'))



Example Using Case in MYSQL Query

 SELECT COUNT(
       CASE
           WHEN roll_no IS NULL
           THEN 1
           ELSE NULL
       END
   ) AS 'not_complete',
   COUNT(
       CASE
           WHEN  roll_no IS NOT NULL
           THEN 1
           ELSE NULL
       END
   ) AS 'completed'
    FROM `application`
WHERE examid =7

 SELECT school_id, COUNT(
       CASE
           WHEN formstatus = '4'
           THEN 1
           ELSE NULL
       END
   ) AS 'Initiated',
   COUNT(
       CASE
           WHEN  formstatus = '45'
           THEN 1
           ELSE NULL
       END
   ) AS 'Payment Pending',
   COUNT(
       CASE
           WHEN  form_status = '55'
           THEN 1
           ELSE NULL
       END
   ) AS 'Payment Done'
    FROM `application` 
WHERE examid =7 GROUP BY school_id

 SELECT COUNT(
CASE 
  WHEN gender = 'MALE'
  THEN 1
  ELSE NULL
  END 
  ) AS MALECOUNT, 
COUNT(
CASE 
  WHEN gender = 'FEMALE'
  THEN 1
  ELSE NULL
  END 
  ) AS FEMALECOUNT, COUNT(*) AS Total 
FROM `application` WHERE exam_id='8' AND form_status IN('55')



Using MYSQL IF statement with Example

 SELECT b.centre_id,COUNT(IF(MED='TAMIL' OR  MED='ENGLISH',1,NULL)) AS TAMENG,
COUNT(IF(MED='KANNADA',1,NULL)) AS KANNADA,
COUNT(IF(MED='HINDI',1,NULL)) AS HINDI,
COUNT(IF(MED='URDU',1,NULL)) AS URDU,
COUNT(IF(MED='ARABIC',1,NULL)) AS ARABIC,
COUNT(IF(MED='PERSIAN',1,NULL)) AS PERSIAN,
COUNT(IF(MED='LATIN',1,NULL)) AS LATIN,
COUNT(IF(MED='ENGLISH',1,NULL)) AS ENGLISH FROM `dummynoexam`a
JOIN `examrollnumber` b ON a.student_id=b.student_id AND a.class_id=b.class_id 
AND a.sess_id=b.sess_id
JOIN  centremasterexam cm FORCE INDEX(cent_class_sess) ON   b.centre_id=cm.CENT 
AND b.class_id=cm.CLASS_ID AND b.sess_id=cm.SESS_ID
GROUP BY SUB,b.centre_id

 SELECT COUNT(IF(statusflag = 1, 1, NULL)) AS STATUS1, COUNT(IF(statusflag=0, 1, NULL)) 
AS STATUS0, COUNT(IF(statusflag IS NULL,1, NULL)) AS STATUSNULL FROM `student`



MYSQL LPAD AND MID FUNCTION() USAGE and Example

SELECT `code`, MID(district_name,1,4) AS prefix, CONCAT(prefix,LPAD(`code`,4,0)) 
FROM `educational_district`


Set the Variable in MYSQL

SET @a:=1;  // Run this first and then run the query

SELECT e.code, s.dge_code, centre_id, rollno, student_group_id, subject_id, medium_id, @a:=@a+1 AS ID   FROM `student_pappermapping` a JOIN `student_exam_details` b ON a.student_exam_details_id =b.id
JOIN `student_details` c ON b.student_id= c.id 
JOIN `subject_list` d ON d.SUBJECT = a.subject_id AND b.class_id=d.CLASS_ID
JOIN school_master s ON c.school_id=s.id
JOIN `educational_district` e ON s.district_id=e.id
JOIN  `exam_rollnumber` r ON r.student_id=c.id AND r.class_id = b.class_id AND r.sess_id=b.sess_id
WHERE c.school_id =1345 AND b.class_id =3 AND b.sess_id= 1 AND (d.TTAG='P' OR LNKSUB <> '')  
ORDER BY e.code, dge_code, student_group_id, subject_id, medium_id



SELECT sr.student_id, doe.ROLLNO, doe.NAME, COUNT(*), (GROUP_CONCAT(sr.subject_id SEPARATOR ', ')) AS applied_subjects
FROM `student_revaluation` sr JOIN `dummy_no_exam` doe ON doe.student_id = sr.student_id AND doe.SUB = sr.subject_id GROUP BY sr.student_id





Reema can complete a piece of work in 12 days while Seema

  1. 👉   Reema can complete a piece of work in 12 days while Seema can the same work in 18 days. If they both work together, then how many days will be required to finish the work?
  1. A. 6 days
  2. B. 7.2 days     ðŸ‘ˆ Correct Answer
  3. C. 9.5 days
  4. D. 12 days

  1. 👉  Answer & Explanation:

    Reema can complete a piece of work in 12 days, So Reema 1 Day's work will be  1/12 
    Seema can complete a Same piece of work in 18 days, So Seema 1 Day's work will be 1 / 18
    Combine 1 Day's  Work of Reema and Seema  ⇒  1/12 +  1/18
    So combined 1 Day work 1/12 +  1/18  ⇒  (6 + 4)/72  ⇒  5 / 36
    : : combinely they (Reema and Seema) will acomplish the task in 36 / 5 Days ⇔ 7(1/5)
    7.2 days
reema can complete a piece of work in 12 days

👉 Reema can complete a piece of work in 12 days 👈



  1. Related Question Like Reema can complete a piece of work:
  1. 20 men and 20 women can complete half of the work in 20 days working with alternatively. If 10 men and 20 boys together can complete the whole work in 10 days, then find the number of days required by 20 boys alone complete the Remaining work?
  1. A. 12 days
  2. B. 11 days
  3. C. 10 days
  4. D. 12 days
  1. Answer : C ( 10 days )

  1. 10 men can complete a piece of work in 15 days and 15 women can complete the same work in 12 days. If all the 10 men and 15 women work together, in how many days will the work get completed?
  1. A. 6
  2. B. 6 (1/3)
  3. C. 6 (2/3)
  4. D. 7 (2/3)
  1. Answer : C ( 6 (2/3) )

  1. Two candles of the same height are lighted at the same time. The first is consumed in 4 h and the second in 3 h. Assuming that each candle burns at a constant rate, in how many hrs after being lighted was the first candle twice the height of the second?
  1. A. 3/4 h
  2. B. 1 1/2 h
  3. C. 2 h
  4. D. 2 2/5 h
  1. Answer : D ( 2 2/5 h )

  1. A can complete 2/3 of the work in 6 days. If A and B together the complete the work in 6 days, then find the time required by C, who is twice efficient than B? June 28, 2018
  1. A. 9 days
  2. B. 7 days
  3. C. 12 days
  4. D. 5 days
  1. Answer : A ( 9 days )

  1. 4 men and 6 women can complete a work in 8 days, while 3 men and 7 women can complete it in 10 days. In how many days will 10 women complete it?
  1. A. 35
  2. B. 40
  3. C. 50
  4. D. 45
  1. Answer : B ( 40 )

  1. Twenty women can do a work in sixteen days. Sixteen men can completed the same work in fifteen days. What is the ratio between the capacity of a man and women?
  1. A. 3:4
  2. B. 4:3
  3. C. 5:3
  4. D. Data inadequate
  1. Answer : B ( 4:3 )

  1. Robin, the cook, can cut either 120 carrots or 72 potatoes or 60 beetroots in t mins. One day, during the first K mins, he spent an equal amount of time cutting each of the three types of vegetables. However, in the next K mins, he cut exactly n carrots, n beetroots and n potatoes. If he cut a total of 282 vegetables during the first 2K mins on that day, what is the value of n?
  1. A. 225
  2. B. 75
  3. C. 45
  4. D. 135
  1. Answer : C ( 45 )

  1. Ten women can complete a piece of work in 15 days. Six men can complete the same piece of work in 10 days. In how many days can five women and six men together complete the piece of work?
  1. A. 15 days
  2. B. 7.5 days
  3. C. 9 days
  4. D. 12.5 days
  1. Answer : B ( 7.5 days )

  1. 7 men can complete a piece of work in 12 days. How many additional men will be required to complete double the work in 8 days?
  1. A. 28
  2. B. 21
  3. C. 14
  4. D. 7
  1. Answer : C ( 14 )

  1. Karthi is 40% more efficient than Rajesh, and Karthi can alone do a work in 20 days. If they working in alternative days starts with Karthi, then in how many days the work will get completed?
  1. A. 21 (1/7) days
  2. B. 26 (2/5) days
  3. C. ys18 (6/17) days
  4. D. 23 (1/5) days

  1. Answer : D ( 23 (1/5) days )

Our Feature Post

There is a tree between houses of A and B If the tree leans on As House

    There is a tree between houses of A and B. If the tree There is a tree between houses of A and B. If the tree leans on A’s House, the t...

Our Popular Post