SQL Queries

download SQL Queries

of 28

description

sql queries

Transcript of SQL Queries

  • 7/31/13 G. SAKTHIVEL MCA

    gsakthimca.blogspot.in 1/28

    F R I D A Y , M A Y 2 7 , 2 0 1 1

    MySQL Tutorials - 112 Queries and Answers

    112 Queries with Answers

    create database 112queries

    use 112queries

    -- Prog Table

    create table prog

    (Pname varchar(20) not null,

    Dob date not null,

    Doj date not null,

    Sex varchar(10) not null,

    Prof1 varchar(10),

    Prof2 varchar(10),

    Sal bigint not null);

    truncate prog;

    insert into prog values('annad',str_to_date('1966-APR-6','%Y-%b-

    %d'),str_to_date('1992-APR-21','%Y-%b-%d'),'m','pascal','basic',3200);

    insert into prog values('sai',str_to_date('1964-JUN-4','%Y-%b-

    %d'),str_to_date('1990-NOV-13','%Y-%b-%d'),'m', 'clipper','cobol',2800);

    insert into prog values('juliana',str_to_date('1968-OCT-6','%Y-%b-

    %d'),str_to_date('1990-APR-21','%Y-%b-%d'),'f','cobol','dbase',3000);

    insert into prog values('kamala',str_to_date('1968-JAN-6','%Y-%b-

    %d'),str_to_date('1992-JAN-02','%Y-%b-%d'),'f','c','dbase',2900);

    insert into prog values('mary',str_to_date('1970-JUN-7','%Y-%b-

    %d'),str_to_date('1991-FEB-01','%Y-%b-%d'),'f','c++','oracle',4500);

    insert into prog values('nelson',str_to_date('1965-SEP-5','%Y-%b-

    %d'),str_to_date('1989-OCT-11','%Y-%b-%d'),'m ','cobol','dbase',2500);

    insert into prog values('qadir',str_to_date('1965-AUG-6','%Y-%b-

    %d'),str_to_date('1993-APR-21','%Y-%b-%d'),'m','assembly','c',3000);

    insert into prog values('ramesh',str_to_date('1967-MAY-7','%Y-%b-

    %d'),str_to_date('1991-FEB-28','%Y-%b-%d'),'m','pascal','dbase',3200 );

    F O L L O W E R S

    Join this sitew ith Google Friend Connect

    Members (2)

    Already a member? Sign in

    B L O G A R C H I V E

    2011 (1)

    May (1)

    MySQL Tutorials - 112 Queries

    and Answers

    2010 (1)

    February (1)

    MySql Tutorial

    2009 (1)

    February (1)

    Lancer

    A B O U T M E

    SA KTHI

    SA LEM , TA M ILNA DU,

    INDIA

    Smart Looking Guy

    With Ever Green

    Smiling Face.

    V IEW M Y COM PLETE PROFILE

    0Share More Next Blog Create Blog Sign In

    G . S A K T H I V E L M C AT HE P O W E R L A N C E R

  • 7/31/13 G. SAKTHIVEL MCA

    gsakthimca.blogspot.in 2/28

    insert into prog values('rebecca',str_to_date('1967-JAN-6','%Y-%b-

    %d'),str_to_date('1990-DEC-28','%Y-%b-%d'),'f','basic','cobol',2500);

    insert into prog values('remitha',str_to_date('1970-APR-7','%Y-%b-

    %d'),str_to_date('1993-APR-20','%Y-%b-%d'),'f','c','assembly',3600);

    insert into prog values('revathi',str_to_date('1969-DEC-9','%Y-%b-

    %d'),str_to_date('1992-JAN-02','%Y-%b-%d'), 'f','pascal','basic',3700);

    insert into prog values('vijaya',str_to_date('1965-DEC-5','%Y-%b-%d'),

    str_to_date('1992-MAY-02','%Y-%b-%d'),'f','foxpro','c',3500);

    select * from prog;

    -- soft Table

    create table soft

    (PNAME VARCHAR(20)NOT NULL,

    TITLE VARCHAR(25)NOT NULL,

    DEV_IN VARCHAR(10)not null,

    SCOST bigint,

    DCOST bigint,

    SOLD bigint);

    insert into soft values('annad', 'parachutes', 'foxpro',2000, 6000, 43);

    insert into soft values('annad','video title','clipper',7500,1600,9);

    insert into soft values('juliana','inventory','clipper', 2000,3500,10);

    insert into soft values('kamala','payroll','dbase',9000,2000,7);

    insert into soft values('mary','financial','c',1800,8500,4);

    insert into soft values('mary','code generator','oracle',4500,2000,23);

    insert into soft values('padric','editor','pascal',750,5000,11);

    insert into soft values('qadir','bombs way','c',499.95,530,114);

    insert into soft values('qadir','vaccines','assembly',3400,3500,21);

    insert into soft values('ramesh','hotel

    management','dbase',2000,3500,4);

    insert into soft values('remitha','pc

    utilities','assembly',3000,3000,31);

    insert into soft values('remitha','tsk help','assembly',2500,6000,6);

    insert into soft values('revathi','hospital management',

    'pascal',1100,7500,2);

    insert into soft values('revathi','quiz master','basic',3200,2100,15);

    insert into soft values('vijaya','tsk editor','c',300,700,6);

  • 7/31/13 G. SAKTHIVEL MCA

    gsakthimca.blogspot.in 3/28

    insert into soft values('mary','read me','c++',1200,1200,84);

    select * from soft;

    -- studies Table

    create table studies

    (Pname varchar(20) not null,

    Splace varchar(20) not null,

    Course varchar(20) not null,

    Ccost bigint not null);

    insert into studies values('qadir', 'apple', 'hdcp', 1400);

    insert into studies values('patrick', 'pargathi', 'dcap', 3000);

    insert into studies values('nelson', 'pargathi', 'dap', 6200);

    insert into studies values('mary', 'sabhari', 'pgdca', 4500);

    insert into studies values('kamala', 'pargathi', 'dcp', 5000);

    insert into studies values('juliana', 'bits', 'mca', 22000);

    insert into studies values('altaf', 'ccit', 'dca', 7200);

    insert into studies values('anand', 'sabari', 'pgdca', 0);

    insert into studies values ('ramesh', 'sabhari', 'pgdca', 4500);

    insert into studies values ('vijaya', 'bdps', 'dca', 48000);

    insert into studies values ('revathi', 'sabhari', 'dap', 5000);

    insert into studies values ('remitha', 'bdpa', 'dcs', 6000);

    insert into studies values ('rebecca', 'brilliant', 'dcap', 11000);

    select * from studies;

    select * from prog;

    select * from soft;

    select * from studies;

    Queries with Answers

    -- 1. Find out selling cost average for packages developed in pascal?

    select avg(scost)

    from soft

  • 7/31/13 G. SAKTHIVEL MCA

    gsakthimca.blogspot.in 4/28

    where dev_in='pascal'

    -- 2. Display the names and ages of all programmers?

    select pname as Name,year(current_date())-year(dob) as Age

    from prog

    -- 3. Display the name of those who have done the dap course?

    select pname

    from studies

    where course='dap'

    -- 4. What is the highest number of copies sold by a package?

    select max(sold)

    from soft

    -- 5. Display the name and date of birth of all programmers born in

    January?

    select pname,dob

    from prog

    where month(dob)=1;

    -- 6. Display the lowest course fee?

    select min(ccost)

    from studies

    -- 7. How many programmers have done the pgdca course?

    select count(*)

    from studies

    where course='pgdca'

    DOUBT -- 8. How much revenue has been earned thru sale of

    packages developed in c?

    -- 9. Display the details of the software developed by ramesh?

    SELECT * FROM SOFT WHERE PNAME='RAMESH'

    -- 10. How many programmers studied at sabhari?

  • 7/31/13 G. SAKTHIVEL MCA

    gsakthimca.blogspot.in 5/28

    SELECT COUNT(*) FROM studies where splace='sabhari'

    -- 11. Display the details of packages whose sales crossed the 20000

    mark?

    select * from soft where sold*scost>20000

    -- 12. Find out the number of copies which should be sold in order to

    recover the development cost of each package?

    select title,count(*)

    from soft

    where (sold*scost)group by title

    -- 13. Display the details of packages for which development cost

    have been recovered?

    select *

    from soft

    where scost*sold >= dcost;

    -- 14. What is the price of quiz master software developed in basic?

    select scost

    from soft

    where title='quiz master' and dev_in='basic';

    -- 15. How many packages were developed in dbase?

    select count(title)

    from soft

    where dev_in='dbase';

    -- 16. How many programmers paid 5000 to 10000 for their course?

    select count(pname)

    from studies

    where ccost between 5000 and 10000;

    -- 17. What is the average course fee?

    select avg(ccost)

  • 7/31/13 G. SAKTHIVEL MCA

    gsakthimca.blogspot.in 6/28

    from studies;

    -- 18. Display the details of programmers knowing c?

    select *

    from prog

    where prof1='c' or prof2='c';

    -- 19. Display the details of programmers knowing only c?

    select *

    from prog

    where prof1='c' and prof2='c';

    -- 20. How many programmer know either cobol or pascal?

    select count(pname)

    from prog

    where (prof1!='cobol' and prof2!='cobol') and (prof1!='pascal' and

    prof2!='pascal');

    -- 21. How many programmers dont know pascal or c?

    select count(pname)

    from prog

    where (prof1'pascal' and prof2'pascal') and (prof1'c' and

    prof2'c');

    -- 22. How old is the oldest male programmers?

    select max(date_format(current_date,'%Y')-date_format(dob,'%Y')) as

    oldestmale from prog

    where sex='m';

    -- 23. What is the average age of female programmers?

    select avg(date_format(current_date,'%Y')-date_format(dob,'%Y')) as

    avg_age from prog

    where sex='f';

    -- 24. Calculate the experience in years for each programmer and

    display along with their names in descending?

    select pname,date_format(current_date,'%Y')-date_format(doj,'%Y')

  • 7/31/13 G. SAKTHIVEL MCA

    gsakthimca.blogspot.in 7/28

    as experience

    from prog

    order by pname desc;

    -- 25. Who are the programmers who celebrate their birth day

    occurring the current month?

    select pname

    from prog

    where month(current_date) = month(dob);

    -- 26. How many female programmers are there?

    select count(pname )as Tot_female

    from prog

    where sex='f';

    -- 27. What is the language known by the female programmer?

    select prof1,prof2

    from prog

    where sex='f';

    -- 28. What is the average salary?

    select avg(sal) from prog;

    -- 29. How many people draw 2000 to 40000?

    select count(*)

    from prog

    where sal between 4000 and 40000

    -- 30. Display the details of those who donot know clipper and oracle?

    select *

    from prog

    where prof1'clipper' and prof1'oracle' and prof2'clipper' and

    prof2'oracle';

    -- 31. How many female programmer knowing c and age below 40?

    select count(pname)

    from prog

    where (sex='f')and (date_format(current_date,'%Y')-

    date_format(dob,'%Y') < 40);

  • 7/31/13 G. SAKTHIVEL MCA

    gsakthimca.blogspot.in 8/28

    -- 32. Who are the programmers wholl be celeberating the current

    month of their joining month?

    select pname

    from prog

    where month(current_date)=month(doj);

    -- Display those with less than 15 year experience?

    select pname

    from prog

    where date_format(current_date,'%Y')-date_format('doj','%Y') = 2;

    -- 35. Calculate the amount to be recovered for those packages

    whose development cost has not yet been recovered?

    select (dcost)-(sold*scost)

    from soft

    where (sold*scost)

    -- 36. List the packages which have been not sold?

    select title

    from soft

    where sold=0;

    -- 37. Find out cost of the software developed?

    select dcost

    from soft;

    -- 38. Which language has been stated as profitioned by the most of

    the programmer?

  • 7/31/13 G. SAKTHIVEL MCA

    gsakthimca.blogspot.in 9/28

    SELECT prof, prof_count

    FROM

    (select prof1 as Prof ,count(prof1) as prof_count from prog group by

    prof1

    union

    select prof2 as Prof ,count(prof2) as prof_count from prog group by

    prof1) as s

    WHERE prof_count = (SELECT MAX(prof_count) FROM

    (select prof1 as Prof ,count(prof1) as prof_count from prog group by

    prof1

    union

    select prof2 as Prof ,count(prof2) as prof_count from prog group by

    prof1) as p);

    -- 39. How many different courses are mentioned in the table?

    select count(distinct course)as Course from studies

    -- 40. Display the names of the programmer whose name contains 5

    chars?

    select pname from prog

    where length(pname)=5;

    -- 41. How many female programmers knowing COBOL have more

    than 2 years experience?

    select count(*)

    from prog

    where (prof1='cobol' or prof2='cobol') and(sex='f') and

    (date_format(current_DATE,'%Y')-date_format(doj,'%Y'))>2;

    -- 42. Display the names of the programmer whose names contain 2

    occurance of letter a?

    select pname

    from prog

    where (LENgth(pname) - LENgth(REPLACE(pname, 'a', ''))) >=2;

    -- 43. What is the length of the shortest name in the programmer

    table?

  • 7/31/13 G. SAKTHIVEL MCA

    gsakthimca.blogspot.in 10/28

    select min(length(pname))

    from prog;

    -- 44. What is the average development cost of the package

    developed in clipper?

    select avg(dcost)

    from soft

    where dev_in = 'clipper';

    -- 45. Display the name, sex, DOB (dd/mm/yy), doj (dd/mm/yy) for

    all programmers without using conversion?

    select pname,sex,date_format(dob,'%d/%m/%y') as

    DOB,date_format(doj,'%d/%m/%y') as DOJ

    from prog;

    -- 46. What is the amount paid in salaries of the all programmer who

    dont know COBOL?

    select pname,sal

    from prog

    where prof1'cobol' and prof2'cobol';

    -- 47. Who is the programmer who was born last day of the month?

    select pname

    from prog

    where dob = last_day(dob);

    -- 48. Display the title scost, dcost and difference between scost and

    dcost in descending of the difference?

    select title,scost,dcost,scost-dcost as diff

    from soft

    order by diff desc;

    -- 49. Display the names of the packages whose name contain more

    than one?

    SELECT SUBSTRING_INDEX(title, ' ',2) from soft;

  • 7/31/13 G. SAKTHIVEL MCA

    gsakthimca.blogspot.in 11/28

    -- 50. Display the name job, DOB, doj of that month of birth and

    month of joining are same?

    select prof1,prof2,dob,doj

    from prog

    where month(dob)=month(doj);

    -- 51. Display the number of the packages developed in each

    language?

    select dev_in,count(title)

    from soft

    group by dev_in;

    -- 52. Display the number of packages developed by each person?

    select pname,count(title)

    from soft

    group by pname;

    -- 53. Display the no of male & female programmer?

    select sex,count(pname)

    from prog

    group by sex;

    -- doubt 54. Display the costliest package and highest selling package

    developed in each language?

    -- 55. Display the no of people born in each year?

    select year(dob),count(pname)

    from prog

    group by year(dob) ;

    -- 56. Display the no of people born in each month?

    select month(dob),count(pname)

    from prog

    group by month(dob) ;

    57. Display the no of people joined in each year?

    select year(doj),count(pname)

  • 7/31/13 G. SAKTHIVEL MCA

    gsakthimca.blogspot.in 12/28

    from prog

    group by year(doj) ;

    -- 58. Display the no of people in each salary group?

    select sal,count(pname)

    from prog

    group by sal ;

    -- 59. Display the language wise count of prof1?

    select prof1,count(pname)

    from prog

    group by prof1

    -- 60. Display the language wise count of prof2?

    select prof2,count(pname)

    from prog

    group by prof2

    -- 61. Display the no of people who studied in each institute?

    select splace,count(pname)

    from studies

    group by splace

    -- 62. Display the no of who studied in each group?

    select course,count(pname)

    from studies

    group by course

    -- 63. Display the selling cost of the packages developed in each

    language?

    select title,dev_in,scost

    from soft

    -- 64. Display the cost of packages developed by each programmer?

    select pname,dcost

    from soft

    -- 65. Display the total development cost of the packages developed

  • 7/31/13 G. SAKTHIVEL MCA

    gsakthimca.blogspot.in 13/28

    in each language?

    select dev_in,sum(dcost)

    from soft

    group by dev_in

    -- 66. Display the number of packages sold by annad?

    select sum(sold)

    from soft

    where pname='annad';

    -- 67. Display the sale?

    select sum(sold)

    from soft;

    -- doubt 68. Display each institute name with no of course and cost

    per course?

    SELECT st.splace, course, ccost, cnt FROM studies st INNER JOIN

    (SELECT splace, COUNT(course) cnt

    FROM studies

    GROUP BY splace) sub

    ON st.splace = sub.splace ORDER BY 1;

    -- 69. Display each institute name with no of students?

    select splace,count(pname)

    from studies

    group by splace;

    -- 70. Display the programmer name and their packages?

    select pname,title

    from soft;

    -- 71. Display the no of packages in each language for which

    development cost is less than 1000?

    select count(title),dev_in

    from soft

    where dcost

  • 7/31/13 G. SAKTHIVEL MCA

    gsakthimca.blogspot.in 14/28

    -- 72. Display the avg difference between scost and dcost?

    select avg(scost-dcost) from soft;

    -- doubt 73. Display the tot scost dcost and the amount to be

    recovered for each programmer for those whose*29?

    select pname,scost*sold,dcost, (scost*sold)-dcost

    from soft where (scost*sold) < dcost

    group by pname;

    -- 74. Display the highest lowest and avg sal for those getting salary

    more than 2000?

    select max(sal),min(sal),(select avg(sal) from prog where sal>2000)

    from prog

    -- 75. Who is the highest paid programmer?

    select pname

    from prog

    where sal = (select max(sal) from prog);

    -- 76. Who is the highest paid female COBOL programmer?

    select pname ,max(sal)

    from prog

    where ( sex='f' )

    and ( prof1='cobol' or prof2='cobol');

    -- 77. Display the names of the highest paid programmer in each

    language?

    SELECT pname, msal, dev FROM prog INNER JOIN

    (SELECT soft.dev_in dev, MAX(prog.sal) msal

    FROM prog, soft

    WHERE prog.pname = soft.pname

    GROUP BY dev_in) sub

    ON prog.sal = sub.msal;

    -- 78. Who is the least experience programmer?

  • 7/31/13 G. SAKTHIVEL MCA

    gsakthimca.blogspot.in 15/28

    select pname

    from prog

    where date_format(current_date,'%Y')-date_format(doj,'%Y')=(select

    min(date_format(current_date,'%Y')-date_format(doj,'%Y'))from

    prog);

    -- 79. Who is the most experience male programmer?

    select pname

    from prog

    where date_format(current_date,'%Y')-date_format(doj,'%Y')=(select

    max(date_format(current_date,'%Y')-date_format(doj,'%Y'))from

    prog)

    and sex='m';

    -- 80. Which language is known by only one programmer?

    SELECT prof FROM

    (SELECT prof1 prof FROM prog

    UNION ALL

    SELECT prof2 FROM prog) sub

    GROUP BY prof

    HAVING COUNT(*) =1;

    -- doubt 81. Who is that above programmer?

    82. Who is the youngest programmer earning more than 3000?

    select pname

    from prog

    where date_format(current_date,'%Y')-date_format(dob,'%Y')=(select

    min(date_format(current_date,'%Y')-date_format(dob,'%Y'))from prog)

    and sal>3000;

    -- 83. Does it know c, c++ oracle or dbase?

    SELECT lang,

    case lang

    when lang in ('c','c++','oracle','dbase') then 'Yes'

    else 'No'

    end as Result

  • 7/31/13 G. SAKTHIVEL MCA

    gsakthimca.blogspot.in 16/28

    FROM

    (SELECT prof as lang FROM

    (SELECT prof1 as prof FROM prog

    UNION ALL

    SELECT prof2 as prof FROM prog) sub

    GROUP BY prof

    HAVING COUNT(*) =1 )as w

    -- 84. Which institutes has the most no of students?

    SELECT splace, COUNT(pname)

    FROM studies

    GROUP BY splace

    HAVING COUNT(pname) = (SELECT MAX(cnt) FROM (SELECT

    COUNT(pname) cnt FROM studies GROUP BY splace) sub)

    -- 85. Display the name of the institute & course which has below

    avg.course fee?

    select splace,course,avg(ccost)

    from studies

    where ccost < (select avg(ccost) from studies)

    group by splace,course

    -- 86. Which is the costliest course?

    select course

    from studies

    where ccost=(select max(ccost)from studies);

    -- 87. Which institute conducts the costliest course?

    select splace

    from studies

    where ccost=(select max(ccost)from studies);

    -- 88. Which course has below avg no of students?

    SELECT course,cnt

    FROM

    (SELECT course,COUNT(pname) cnt FROM studies GROUP BY course)

    AS a

    WHERE cnt

  • 7/31/13 G. SAKTHIVEL MCA

    gsakthimca.blogspot.in 17/28

    FROM studies GROUP BY course)AS b)

    -- 89. Which institute conducts the above course?

    select s.splace,sub.course

    from studies as s

    inner join

    (SELECT course,cnt

    FROM

    (SELECT course,COUNT(pname) cnt FROM studies GROUP BY course)

    AS a

    WHERE cnt

  • 7/31/13 G. SAKTHIVEL MCA

    gsakthimca.blogspot.in 18/28

    -- 93. Who developed the packages that have sold the least no of

    copies?

    select pname

    from soft

    where sold = (select min(sold) from soft);

    -- 94. Which languages were used to develop the package which has

    highest sales amount?

    select dev_in

    from soft

    where scost = (select max(scost) from soft);

    -- 95. How many copies of the packages that have the least

    difference

    between development & selling cost were sold?

    select title

    from soft

    where scost-dcost= (select min(scost-dcost) from soft)

    -- 96. Which are costliest packages developed in pascal?

    select title

    from soft

    where dcost=(select max(dcost) from soft where dev_in='pascal')

    -- doubt 97. Which languages were used to develop the most no of

    packages?

    select dev_in as Lang

    from

    (select dev_in,count(title) as t_count

    from soft

    group by dev_in)as res

    where res.t_count=(select max(t_count) from (

    (select dev_in,count(title) as t_count

    from soft

    group by dev_in)as res))

  • 7/31/13 G. SAKTHIVEL MCA

    gsakthimca.blogspot.in 19/28

    -- 99. Display the names of the packages

    -- which have sold less than the avg no copies?

    select title

    from soft

    where sold(2*dcost)

    select pname,scost,(2*dcost)

    from soft

    where scost >(2*dcost)

    -- doubt 101. Display the programmer names & the cheapest package

    developed

    -- by them in each language?

    select pname,title,min(scost)

    from soft

    where scost = (select min(scost)from soft)

    group by dev_in

    select pname,title,dev_in,min(scost)

    from soft

    group by dev_in

    where scost = (select min(scost)from soft)

    select count(pname) from soft;

    -- doubt 102. Display the language used by each programmer

    -- to develop the highest selling & lowest selling packages?

    -- 103. Who is the youngest male programmer born in 1965?

    select pname

    from prog

    where (date_format(dob,'%m')= ( select max(date_format(dob,'%m'))

    from prog

    where sex='m'))

  • 7/31/13 G. SAKTHIVEL MCA

    gsakthimca.blogspot.in 20/28

    and (date_format(dob,'%Y')='1965')

    -- 104. Who is the oldest female programmer who joined in 1992?

    select pname

    from prog

    where (date_format(doj,'%m')= ( select min(date_format(doj,'%m'))

    from prog

    where sex='f'))

    and (date_format(doj,'%Y')='1992')

    -- 105. In which year were most no of programmer born?

    select count(pname),year(dob)

    from prog

    group by year(dob)

    -- doubt 106. In which language are most no of programmer

    proficiency?

    select prof,cnt

    from

    (select prof1 as prof ,count(prof1) as cnt from prog group by prof1

    union

    select prof2 as prof ,count(prof2) as cnt from prog group by prof1) as

    t1

    where cnt=(select max(cnt) from

    (select prof1 as prof ,count(prof1) as cnt from prog group by prof1

    union

    select prof2 as prof ,count(prof2) as cnt from prog group by prof1) as

    t2)

    -- 107. Who are the male programmers earning below the avg sal of

    female programmer?

    select pname

    from prog

    where sex = 'm' and sal

  • 7/31/13 G. SAKTHIVEL MCA

    gsakthimca.blogspot.in 21/28

    where sex = 'm' and sal(select max(sal) from prog where sex='m')

    select pname,sal

    from prog

    where sex = 'f' and sal>(select max(sal) from prog where sex='m')

    -- 109. Display the details of the software developed in female

    programmer in paragathi?

    select a.pname,a.sex,b.title,b.dev_in,b.scost,c.splace

    from soft as b join prog as a join studies as c

    where a.pname = b.pname and b.pname=c.pname

    and a.sex='f' and c.splace='pargathi'

    -- doubt 110. Display the details of the software developed by the

    male programmer born before 1965

    -- and female programmer born after 1975?

    select a.pname,a.title,a.dev_in,a.scost,a.dcost,b.sex,b.dob

    from soft as a join prog as b

    where b.pname =a.pname or

    (year(b.dob)

  • 7/31/13 G. SAKTHIVEL MCA

    gsakthimca.blogspot.in 22/28

    W E D N E S D A Y , F E B R U A R Y 1 0 , 2 0 1 0

    MySql Tutorial

    MySql Tutorial

    SQLStatement

    Syntax

    AND / OR SELECT column_name(s) FROM table_name WHERE condition

    ANDOR condition

    ALTER

    TABLE

    ALTER TABLE table_name

    ADD column_name datatype

    or

    ALTER TABLE table_name

    DROP COLUMN column_name

    AS (alias) SELECT column_name AS column_alias FROM table_name

    or

    SELECT column_name

    FROM table_name AS table_alias

    BETWEEN SELECT column_name(s) FROM table_name

    WHERE column_name BETWEEN value1 AND value2

    CREATE CREATE DATABASE database_name

  • 7/31/13 G. SAKTHIVEL MCA

    gsakthimca.blogspot.in 23/28

    DATABASE

    CREATETABLE

    CREATE TABLE table_name (

    column_name1 data_type, column_name2 data_type,

    column_name2 data_type, ... )

    CREATEINDEX

    CREATE INDEX index_name ON table_name (column_name)

    or

    CREATE UNIQUE INDEX index_name

    ON table_name (column_name)

    CREATE

    VIEW

    CREATE VIEW view_name AS

    SELECT column_name(s) FROM table_name WHERE condition

    DELETE DELETE FROM table_name WHERE some_column=some_value

    Or

    DELETE FROM table_name (Note: Deletes the entire table!!)

    DELETE * FROM table_name (Note: Deletes the entire table!!)

    DROP

    DATABASE

    DROP DATABASE database_name

    DROPINDEX

    DROP INDEX table_name.index_name (SQL Server) DROP INDEX index_name ON table_name (MS

    Access) DROP INDEX index_name (DB2/Oracle) ALTER TABLE table_name DROP INDEX index_name (MySQL)

    DROPTABLE

    DROP TABLE table_name

    GROUP BY SELECT column_name,aggregate_function(column_name) FROM table_name

  • 7/31/13 G. SAKTHIVEL MCA

    gsakthimca.blogspot.in 24/28

    WHERE column_name operator value

    GROUP BY column_name

    HAVING SELECT column_name,aggregate_function(column_name)

    FROM table_name WHERE column_name operator value GROUP BY column_name HAVING aggregate_function(column_name) operator

    value

    IN SELECT column_name(s) FROM table_name

    WHERE column_name IN (value1,value2,..)

    INSERTINTO

    INSERT INTO table_name VALUES (value1, value2, value3,....)

    or

    INSERT INTO table_name (column1, column2, column3,...)

    VALUES (value1, value2, value3,....)

    INNERJOIN

    SELECT column_name(s) FROM table_name1

    INNER JOIN table_name2 ONtable_name1.column_name=table_name2.column_name

    LEFT JOIN SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2 ON

    table_name1.column_name=table_name2.column_name

    RIGHTJOIN

    SELECT column_name(s) FROM table_name1

    RIGHT JOIN table_name2 ONtable_name1.column_name=table_name2.column_name

    FULL JOIN SELECT column_name(s) FROM table_name1 FULL JOIN table_name2 ON

    table_name1.column_name=table_name2.column_name

    LIKE SELECT column_name(s)

  • 7/31/13 G. SAKTHIVEL MCA

    gsakthimca.blogspot.in 25/28

    FROM table_name

    WHERE column_name LIKE pattern

    ORDER BY SELECT column_name(s) FROM table_name ORDER BY column_name [ASCDESC]

    SELECT SELECT column_name(s) FROM table_name

    SELECT * SELECT * FROM table_name

    SELECTDISTINCT

    SELECT DISTINCT column_name(s) FROM table_name

    SELECT

    INTO

    SELECT *

    INTO new_table_name [IN externaldatabase] FROM old_table_name

    or

    SELECT column_name(s) INTO new_table_name [IN externaldatabase]

    FROM old_table_name

    SELECTTOP

    SELECT TOP numberpercent column_name(s) FROM table_name

    TRUNCATETABLE

    TRUNCATE TABLE table_name

    UNION SELECT column_name(s) FROM table_name1 UNION SELECT column_name(s) FROM table_name2

    UNIONALL

    SELECT column_name(s) FROM table_name1 UNION ALL SELECT column_name(s) FROM table_name2

    UPDATE UPDATE table_name SET column1=value, column2=value,...

    WHERE some_column=some_value

    WHERE SELECT column_name(s) FROM table_name

    WHERE column_name operator

    Date and Time Functions

  • 7/31/13 G. SAKTHIVEL MCA

    gsakthimca.blogspot.in 26/28

    ADDDATE() Add dates ADDTIME() Add time CONVERT_TZ() Convert from one timezone to another CURDATE() Return the current date

    CURRENT_DATE(),

    CURRENT_DATE Synonyms for CURDATE() CURRENT_TIME(),

    CURRENT_ Synonyms for CURTIME() CURRENT_TIMESTAMP(),

    CURRENT_TIMESTAMP Synonyms for NOW() CURTIME() Return the current time DATE_ADD() Add two dates DATE_FORMAT() Format date as specified DATE_SUB() Subtract two dates

    DATE() Extract the date part of a date or datetime expression DATEDIFF() Subtract two dates DAY() Synonym for DAYOFMONTH() DAYNAME() Return the name of the weekday

    DAYOFMONTH() Return the day of the month (1-31) DAYOFWEEK() Return the weekday index of the argument DAYOFYEAR() Return the day of the year (1-366) EXTRACT Extract part of a date FROM_DAYS() Convert a day number to a date

    FROM_UNIXTIME() Format date as a UNIX timestamp GET_FORMAT() Return a date format string HOUR() Extract the hour LAST_DAY Return the last day of the month for the argument

    LOCALTIME(),

    Synonym for NOW()

    LOCALTIMESTAMP,

    LOCALTIMESTAMP() Synonym for NOW() MAKEDATE() Create a date from the year and day of year

    MAKETIME Returns a time value calculated from the hour, minute,and second arguments MICROSECOND() Return the microseconds from argument MINUTE() Return the minute from the argument MONTH() Return the month from the date passed

    MONTHNAME() Return the name of the month NOW() Return the current date and time PERIOD_ADD() Add a period to a year-month

  • 7/31/13 G. SAKTHIVEL MCA

    gsakthimca.blogspot.in 27/28

    PERIOD_DIFF() Return the number of months between periods

    QUARTER() Return the quarter from a date argument SEC_TO_TIME() Converts seconds to 'HH:MM:SS' format SECOND() Return the second (0-59) STR_TO_DATE() Convert a string to a date SUBDATE() When invoked with three arguments a synonym for

    DATE_SUB() SUBTIME() Subtract times SYSDATE() Return the time at which the function executes TIME_FORMAT() Format as time TIME_TO_SEC() Return the argument converted to seconds

    TIME() Extract the time portion of the expression passed TIMEDIFF() Subtract time TIMESTAMP() With a single argument, this function returns thedate or datetime expression.

    With two arguments, the sum of the arguments TIMESTAMPADD() Add an interval to a datetime expression TIMESTAMPDIFF() Subtract an interval from a datetimeexpression TO_DAYS() Return the date argument converted to days

    UNIX_TIMESTAMP() Return a UNIX timestamp UTC_DATE() Return the current UTC date UTC_TIME() Return the current UTC time UTC_TIMESTAMP() Return the current UTC date and time WEEK() Return the week number

    WEEKDAY() Return the weekday index WEEKOFYEAR() Return the calendar week of the date (1-53) YEAR() Return the year YEARWEEK() Return the year and week

    POSTED BY SAKTHI AT 3:45 AM NO COM M ENTS: LINKS TO THIS POST

    LABELS: M YSQL, M YSQL TUTORIALS, M YSQLTUTORIALS

    T H U R S D A Y , F E B R U A R Y 2 6 , 2 0 0 9

    Lancer

    lancer

    A cavalryman armed with a lance.

    A member of a regiment originally armed with lances.

    The lancer have the power of lance in wind....!

    POSTED BY SAKTHI AT 12:00 AM NO COM M ENTS: LINKS TO THIS POST

  • 7/31/13 G. SAKTHIVEL MCA

    gsakthimca.blogspot.in 28/28

    Home

    Subscribe to: Posts (Atom)