Query to find the class name that has the maximum number of students ?

I need the query to find the [class Name] which has the most number of students. please look below at the tables
Students Table                                                                     
StudentId     StudentName  ClassID                                    
1                  xxx                   1                                           
2                  yyy                   1                                           
3                  zzz                   1  
4                  fff                    2
5                   ttt                  2
 Classes Table
 ClassID          ClassNane
 1                    CSHARP
 2                    JSHARP
The result should be : CSHARP
since there are 3 students in CSHARP and 2 students in JSHARP class
Appreciate your help
Thanks

Try:
DECLARE @Classes TABLE (
ClassID INT identity(1, 1) PRIMARY KEY
,ClassName VARCHAR(50)
INSERT INTO @Classes (ClassName)
VALUES ('CSharp')
,('JSharp')
DECLARE @Students TABLE (
StudentID INT identity(1, 1) PRIMARY KEY
,StudentName VARCHAR(10)
,ClassID INT
INSERT INTO @Students (
StudentName
,ClassID
VALUES (
'xxx'
,1
'yyy'
,1
'zzz'
,1
'fff'
,2
'ttt'
,2
SELECT TOP (1)
WITH TIES C.ClassName
FROM @Classes C
INNER JOIN @Students S ON C.ClassID = S.ClassID
GROUP BY C.ClassID
,C.ClassName
ORDER BY COUNT(S.StudentID) DESC
For every expert, there is an equal and opposite expert. - Becker's Law
My blog
My TechNet articles

Similar Messages

  • Query to find the maximum and second maximum

    i have a table temptab of 3 columns
    col1 col2 col3
    1 a 08-JAN-08
    1 a 09-JAN-08
    1 a 10-JAN-08
    1 b 10-JAN-08
    1 c 11-mar-08
    1 c 10-mar-08
    i want to select 1st maxm and 2nd maxm of col3 group by (col1,col2)
    o/p will be like
    1 a 10-jan-08 08-jan-08
    1 b 10-jan-08 null
    1 c 11-mar-08 10-mar-08
    select first.col1, first.col2, first.MAX_DATE, second.SEC_DATE from (select a.col1, a.col2, max(a.col3) as MAX_DATE
    from tab1 a group by a.col1, a.col2) first,
    (select b.col1, b.col2, max(b.col3) as SEC_DATE from tab1 b,
    (select a.col1, a.col2, max(a.col3) as MAX_DATE from tab1 a
    group by a.col1, a.col2) c
    WHERE c.col1 = b.col1
    AND c.col1=b.col1
    AND c.MAX_DATE > b.col3
    group by b.col1, b.col2) second
    where first.col1 = second.col1
    and first.col2 = second.col2;
    this is not working..
    please give a query which will do this.

    1. Your query have 3 subqueries where mine only one. Feel free to make your choice regarding the performance.
    2. You cannot have a column named date, unless you use double-quotes
    3. Your query doesn't work as well when you have not any second highest value
    4. Your query return a cartesian product of your table.
    Please check the results of your query (after applying the required modications) with the mine above, and make your choice :
    SQL> with tbl as
      2  (select 1 col1, 'a' col2, to_date('08-JAN-08','DD-MON-YY') col3 from dual union all
      3   select 1 col1, 'a' col2, to_date('09-JAN-08','DD-MON-YY') col3 from dual union all
      4   select 1 col1, 'a' col2, to_date('10-JAN-08','DD-MON-YY') col3 from dual union all
      5   select 1 col1, 'b' col2, to_date('10-JAN-08','DD-MON-YY') col3 from dual union all
      6   select 1 col1, 'c' col2, to_date('11-mar-08','DD-MON-YY') col3 from dual union all
      7   select 1 col1, 'c' col2, to_date('10-mar-08','DD-MON-YY') col3 from dual )
      8  --end of data sample
      9  select t1.col1 ,t1.col2 ,t1.col3,t2.col3
    10  from (select col1,col2,max(col3) col3
    11        from tbl
    12        group by col1,col2 ) t1,
    13       (select col1, col2, col3
    14        from (select col1, col2, col3,
    15                     row_number() over (partition by col1, col2 order by col3 desc) rn
    16              from tbl)
    17       where rn <= 2) t2
    18  where t1.col3 !=t2.col3;
          COL1 C COL3            COL3
             1 c 11-MAR-08       10-JAN-08
             1 a 10-JAN-08       09-JAN-08
             1 b 10-JAN-08       09-JAN-08[i] <-- obviously wrong, there is no 09-JAN-08 for 1b
             1 c 11-MAR-08       09-JAN-08[i] <-- obviously wrong, there is no 09-JAN-08 for 1c
             1 c 11-MAR-08       10-JAN-08[i] <-- obviously wrong, there is no 10-JAN-08 for 1c
             1 a 10-JAN-08       11-MAR-08[i] <-- obviously wrong, there is no 11-MAR-08 for 1a
             1 b 10-JAN-08       11-MAR-08[i] <-- obviously wrong, there is no 11-MAR-08 for 1b
             1 a 10-JAN-08       10-MAR-08[i] <-- obviously wrong, there is no 10-MAR-08 for 1a
             1 b 10-JAN-08       10-MAR-08[i] <-- obviously wrong, there is no 10-MAR-08 for 1b
             1 c 11-MAR-08       10-MAR-08[i] <-- obviously wrong, duplicate row
    10 rows selected.Nicolas.

  • Query to find the no of columns in a table

    Is there a query to find the total number of columns in a table?

    Dou you know read?
    -- the number of rows in a table named yourtable:
    select count(1) from yourtable;
    -- the number of columns in a table
    -- If is a user's table:
    select count(1)
    from user_tab_columns
    where table_name='YOURTABLE'
    --If it is´t
    select count(1)
    from DBA_tab_columns
    where owner = 'USEROWNER'
    and table_name='YOURTABLE'

  • How to find the maximum no of users logged in Database

    Hi All,
    How to find the maximum number of users logged in database at particular time? I can find currently logged users or umber of sessions running on Database but I wanted to find at what time maximum number of users or sessions connected to the DB? is that possible? if yes please help me. thanks.

    Vijay wrote:
    Hi All,
    How to find the maximum number of users logged in database at particular time? I can find currently logged users or umber of sessions running on Database but I wanted to find at what time maximum number of users or sessions connected to the DB? is that possible? if yes please help me. thanks.
    select * from v$resource_limit;Lists about 20 different resources with current, maximum and limit on usage. Session count is one of them. I don't think there's any (simple, efficient) way to determine the maximum number of different users of sessions, though.
    Regards
    Jonathan Lewis

  • Query to find the Views and synonyms that are accessing through db_link

    HI all,
    Oracle 10g
    I need a Query to find the Views and synonyms that are accessing through db_link.
    ie.
    database A have the db_link to database B through a schema A
    now i need to find what are the Synonyms and views that are accessing through db_link either directly or indirectly..
    regards,
    Deepak
    Edited by: Deepak_DBA on Dec 10, 2010 5:38 PM

    On the second database (B) use this query to find the SQL which used by the schema A (DB LINK USER). Check the SQL_FULLTEXT column.
    select sql_fulltext,sql_id,module,parsing_schema_name,parsing_user_id,first_load_time,loads,users_executing,rows_processed,plsql_exec_time,sorts,fetches,invalidations,parse_calls,cpu_time,elapsed_time,disk_reads,buffer_gets
    from V$sqlarea
    where parsing_schema_name = 'A' --and to_char(first_load_time,'dd/mm/yyyy') like  '%11/08/2007'
    order by first_load_time desc;
    Regards
    Asif Kabir

  • What's the query to find all requests' name of all modules EBS R12.1.3??

    What's the query to find all requests' name of all modules EBS R12.1.3??
    Regards:
    Shahzad M. Saleem

    Dear rioman !!!
    Thanks!!
    Regards:
    Shahzad M. Saleem

  • HT4759 How can I find the serial number toy device that has been stolen? I have looked in my account on iTunes and go to manage devices but only option is to delete that device?

    My daughters iPod was stolen today and I disabled it and tracked it on the find iPhone to an address but law enforcement can't do anything yet since they have now turned the iPod off which means it's off the radar now but for the life of me I can not find the serial number in my iTunes account to give the investigator so that he can put the info into a data base for pawn shops. Help???

    Try here >  How to find the serial number of your Apple hardware product
    I hope you find it.

  • HT1267 someone has found my lost Ipad3 and changed he name.  How do i find the serial number?

    I left my Ipad (3 the new one) on an airplane a couple weeks ago.  How do I remotely find the serial number to register it as lost?  Can the person who picked it up access it even though I've remotely locked it?

    No, they cannot access the data on your iPad, though they can wipe it, which would also wipe the passcode.
    The iPad should be registered to you, so go to http://supportprofile.apple.com and that will tell you the serial number.

  • Query to find the Salary table details  HRMS 11i

    I am looking  query to find the persons salary details in Oralce EBS 11i.
    I tried the below query bu it didn'ty work.
    SELECT papf.employee_number
    ,papf.full_name
    ,pj.NAME job
    ,haou.NAME ORGANIZATION
    ,ppp.proposed_salary_n salary
    FROM per_all_people_f papf
    ,per_all_assignments_f paaf
    ,per_jobs pj
    ,hr_all_organization_units haou
    ,per_position_definitions ppd
    ,per_all_positions pap
    ,per_pay_proposals ppp
    WHERE 1 = 1
    AND SYSDATE BETWEEN papf.effective_start_date AND papf.effective_end_date
    AND papf.current_employee_flag = 'Y'
    AND papf.employee_number IS NOT NULL
    AND paaf.person_id = papf.person_id
    AND SYSDATE BETWEEN paaf.effective_start_date AND paaf.effective_end_date
    AND paaf.job_id = pj.job_id
    AND paaf.organization_id = haou.organization_id
    AND paaf.position_id = pap.position_id
    AND pap.position_definition_id = ppd.position_definition_id
    AND ppp.pay_proposal_id = (SELECT MAX (pay_proposal_id)
    FROM per_pay_proposals
    WHERE assignment_id = paaf.assignment_id)
    In our case all the below tables have 0 records..
    select count(*) from PER_PAY_PROPOSALS ;
      COUNT(*)
             0
    select count(*) from pay_element_entry_values_f;
      COUNT(*)
             0
    select count(*) from PAY_ELEMENT_ENTRY_VALUES_F;
      COUNT(*)
             0
    select count(*) from PAY_ELEMENT_ENTRIES_F;
      COUNT(*)
             0

    Hi,
    Your results clearly states that there is no salary data which is been captured as well as there are no element entries which are assigned to any assignment.
    Please do the below which will insert records in salary as well as element entries table:
    1. Add a salary proposal to any active employee (People Enter and Maination --> Search for any employee --> Assignment --> Salary) - This will insert a record in PER_PAY_PROPOSALS table
    2. Add an element entry to any active assignment (People Enter and Maination --> Search for any employee --> Assignment --> Entries) - This will insert a record in PAY_ELEMENT_ENTRIES_F and PAY_ELEMENT_ENTRY_VALUES_F table
    Hope this clarifies.
    Thanks,
    Sanjay

  • I'm using acrobat pro in my project after debuging the project and after opening a certain number of PDF files I receive the message: the maximum number of files opened has been reached, you have to close some files to continu.even doing that, I steel rec

    I'm using acrobat pro in my project after debuging the project and after opening a certain number of PDF files I receive the message: the maximum number of files opened has been reached, you have to close some files to continu.even doing that, I steel receive the same message.Some one can tel what to do please? Thanks

    Hi Memalyn
    Essentially, the bare issue is that you have a 500GB hard drive with only 10GB free. That is not sufficient to run the system properly. The two options you have are to move/remove files to another location, or to install a larger hard drive (eg 2TB). Drive space has nothing to do with SMC firmware, and usually large media files are to blame.
    My first recommendation is this: download and run the free OmniDiskSweeper. This will identify the exact size of all your folders - you can drill down into the subfolders and figure out where your largest culprits are. For example, you might find that your Pictures folder contains both an iPhoto Library and copies that you've brought in from a camera but are outside the iPhoto Library structure. Or perhaps you have a lot of purchased video content in iTunes.
    If you find files that you KNOW you do not need, you can delete them. Don't delete them just because you have a backup, since if the backup fails, you will lose all your copies.
    Don't worry about "cleaners" for now - they don't save much space and can actually cause problems. Deal with the large file situation first and see how you get on.
    Let us know what you find out, and if you manage to get your space back.
    Matt

  • How can I find the serial number for my stolen iPad that's associated with my apple ID ?

    How can I find the serial number for my stolen iPad that's associated with my apple ID ?

    If you registered it with Apple check your registered items at Your Support Profile for Registered Purchases. Also, see iOS- How to find the serial number, IMEI, MEID, CDN, and ICCID number.
    What To Do If Your iDevice Is Lost Or Stolen
    If you activated Find My Phone before it was lost or stolen, you can track it only if Wi-Fi is enabled on the device. What you cannot do is track your device using a serial number or other identifying number. You cannot expect Apple or anyone else to find your device for you. You cannot recover your loss unless you insure your device for such loss. It is not covered by your warranty.
    If your iPhone, iPod, iPod Touch, or iPad is lost or stolen what do you do? There are things you should have done in advance - before you lost it or it was stolen - and some things to do after the fact. Here are some suggestions:
    This link, Re: Help! I misplaced / lost my iPhone 5 today morning in delta Chelsea hotel downtown an I am not able to track it. Please help!, has some good advice regarding your options when your iDevice is lost or stolen.
      1. Reporting a lost or stolen Apple product
      2. Find my lost iPod Touch
      3. AT&T. Sprint, and Verizon can block stolen phones/tablets
      4. What-To-Do-When-Iphone-Is-Stolen
      5. What to do if your iOS device is lost or stolen
      6. 6 Ways to Track and Recover Your Lost/Stolen iPhone
      7. Find My iPhone
      8. Report Stolen iPad | Stolen Lost Found Online
    It pays to be proactive by following the advice on using Find My Phone before you lose your device:
      1. Find My iPhone
      2. Setup your iDevice on iCloud
      3. OS X Lion/Mountain Lion- About Find My Mac
      4. How To Set Up Free Find Your iPhone (Even on Unsupported Devices)

  • Query to find the latest record with respect to the current status

    Dear gurus
    I have the following data in a table
    Customernum
    bkcode
    reqtdate
    Prevstat
    currstat
    The data will be like this
    CustomerNum bkcode reqdate prevstat currstat
    5900 1 03-Aug-12 0 1
    5900 1 06-Aug-12 1 0
    5900 5 22-Jun-12 0 1
    If a customer has an issue to solved, a record is added with bkcode , register date and currstat will be 1
    If the issue is resolved for the bookingcode,a new record is added, the currentstatus will become 0. and prev stat will show 1. Row no 1 and 2 reflects this case
    If this table is queried for finding the unresolved issues. the output should be only the Last row of the above example. since issue with bookingcode 1 has been resolved
    I have trying hard to get this thing confused what to use Lead or Max
    Kindly guide me

    Hi,
    one way here:
    WITH mytable(CustomerNum, bkcode, reqdate, prevstat, currstat)
    AS
       SELECT 5900, 1, TO_DATE('03-Aug-12', 'DD-Mon-YY'), 0, 1 FROM DUAL UNION ALL
       SELECT 5900, 1, TO_DATE('06-Aug-12', 'DD-Mon-YY'), 1, 0 FROM DUAL UNION ALL
       SELECT 5900, 5, TO_DATE('22-Jun-12', 'DD-Mon-YY'), 0, 1 FROM DUAL
    SELECT CustomerNum, bkcode, reqdate, prevstat, currstat
      FROM (SELECT a.*
                 , ROW_NUMBER() OVER (PARTITION BY CustomerNum, bkcode
                                           ORDER BY reqdate DESC) AS rn
              FROM mytable a
    WHERE rn=1
       AND currstat=1;
    CUSTOMERNUM     BKCODE REQDATE     PREVSTAT   CURRSTAT
           5900          5 22-JUN-12          0          1Regards.
    Al
    Edited by: Alberto Faenza on Dec 18, 2012 5:23 PM
    Changed again!! Previous logic was wrong

  • Sql query to find the balances for a customer account wise.

    Hi,
    Could someone help me with the sql query to find the balances for each customer account wise. This is need to generate the report.
    presently we are using this query, but the output doesnot return the expected result.
    SELECT sum(nvl(ps.acctd_amount_due_remaining,0)) "Balance"
    FROM      ra_cust_trx_line_gl_dist_all gld,
              gl_code_combinations c,
              ar_payment_schedules_all ps,
              RA_CUSTOMER_TRX_ALL rat,
              ra_customers rc
    WHERE      c.CHART_OF_ACCOUNTS_ID = 101
    and gld.code_combination_id = c.code_combination_id
         and rat.CUSTOMER_TRX_ID=gld.CUSTOMER_TRX_ID
         and rat.CUSTOMER_TRX_ID=ps.CUSTOMER_TRX_ID
    and ps.customer_id=rc.customer_id
         and ps.status='OP'
         and ps.gl_date <= :PDATE
         and ps.org_id=:PORGID
         and ps.class in ('GUAR','INV','DM','DEP')
    and c.SEGMENT4=:Account_id
    and ps.customer_id=:Customer_id
    Thanks in advance.
    Kalyan.

    Can someone help us with this.

  • Can anybody provide the SQL query to find the files uploaded in a particular folder?

    Hi All,
    Can anybody provide the SQL query to find the documents (document name) uploaded in a particular folder? While clicking on folder in
    GUI I'm hitting the Timeout error. I would like to find the files uploaded into this folder from SQLPLUS.
    Any help is greatly appreciated.
    With best regards,
    Nevin

    Nevin,
    Be great if we could know the version of Portal. For Rel. 1, here's the query
    select id,masterthingid from wwv_things
    where siteid = &site
    and cornerid = &corner
    &site - Content Area id
    &corner - Folder id
    if you don't know the folder id, use
    select id from wwv_corners where siteid = &site
    and name = &folder
    Hope this helps. I have run into this situation before. Usually, the culprits were
    one of the following:
    1. Junk Characters in description of item (caused due to Copy-Paste)
    2. Special Characters in the File name
    Hi All,
    Can anybody provide the SQL query to find the documents (document name) uploaded in a particular folder? While clicking on folder in
    GUI I'm hitting the Timeout error. I would like to find the files uploaded into this folder from SQLPLUS.
    Any help is greatly appreciated.
    With best regards,
    Nevin

  • SQL Query to find the Notify upon Completion employees

    Hi,
    I am looking for a query to find the names of the employees/users who are notified upon the completion of the Concurrent Programs. I know how to find the concurrent program details but not able to find notify upon completion employees details. Please help me.
    Thanks,
    KM

    Thanks Scott for responding. There is no any custom code. Here is the navigation to reach there:
    System Administrator -> Requests -> View -> Specific Requests -> Open any request ->View Details -> Options -> Notify the following people
    I am looking for the query to find these notified people using SQL Query.Query FND_CONCURRENT_REQUESTS table (NOTIFY_ON_PP_ERROR column) -- http://etrm.oracle.com/pls/et1211d9/etrm_pnav.show_object?c_name=FND_CONCURRENT_REQUESTS&c_owner=APPLSYS&c_type=TABLE
    Thanks,
    Hussein

Maybe you are looking for

  • HP 4600n Color Laser Jet

    When powering up the HP 4600n color laser jet printer, it stops at 64mb************* What does this mean

  • Partner role

    Hi all,   Im new to IS-OIL. Can anyone explain what is this partner role and location and what is the use of it? I tried to understand the same through SAP documents. but i didn't understand. can some one explain in simple terms? Regards, Rahul

  • Level of Maintenance

    Hi. I´ve tried to upgrade from JDeveloper 11.1.2.3 to 11.1.2.4.0, but the installer gives a message that I must have one with the correct level of maintenance (the 11.1.2.4.0 is incompatible with the installed one). What can be the usual suspects of

  • I fell and crack the back of my iPhone 4S now the phone and 4S quit working.

    Tried restoring, and taking out SIM card didnt work. Can Apple do anything to help or will I have to buy a new phone

  • Captivate 8 - Slide timing & interaction questions

    I used to work somewhat with Captivate 3 & 4, and now just got access to Captivate 8 and am currently in the process of evaluating it.  I am hoping someone in the community could help me with the following questions: 1. Is there a way to add more tha