Weather Observation Station 9

  • + 36 comments

    MySQL solution

    From my HackerRank solutions.

    SELECT DISTINCT CITY FROM STATION
    WHERE CITY REGEXP '^[^aeiou]';
    

    Putting a ^ inside the closed bracket means something completely different than putting it outside the brackets. Putting it inside the brackets makes it match all characters EXCEPT the ones inside the bracket. So instead of writing [bcdfghjklmnpqrstvwxyz], we can write [^aeiou]

    • + 5 comments

      why did you include ^ outside the closed bracket..

      • + 5 comments

        Putting a ^ inside the closed bracket means something completely different than putting it outside the brackets. Putting it inside the brackets makes it match all characters EXCEPT the ones inside the bracket. So instead of writing [bcdfghjklmnpqrstvwxyz], we can write [^aeiou]

        HackerRank solutions.

        • + 4 comments

          yeah as i did write [^aeiou] this but the query failed .. it only worked after i included ^[^aeiou]. so this means query the names of all the city which are not starting from vowel s we could have just used [^aeiou] this .. but it didnt worked explain please

          • + 8 comments

            The first ^ is to make it match the beginning of the String. The second ^ is negation. You need both to work.

            If you just did [^aeiou] it would be strings without aeiou. You need the extra ^ in ^[^aeiou] to make sure to match strings that dont BEGIN with aeiou.

            HackerRank solutions.

            • + 3 comments

              could yout tell me where can I read about REGEXP as i m new to mysql

              • + 4 comments

                Intro to Regular Expressions

                This link is a nice, brief, intro. It says for google analytics, but it covers the basic symbols, and is MUCH more readable than lots of other resources.

                • + 0 comments

                  thanks

                • + 0 comments

                  Thank u!

                • + 0 comments

                  Thanks a lot :)

                • + 0 comments

                  Thanks !

              • + 0 comments

                https://www.hackerrank.com/domains/regex/re-introduction

              • + 0 comments

                I think this is a good explaination for regexp.

                Oracle:https://www.techonthenet.com/oracle/regexp_like.php

                Mysql:https://dev.mysql.com/doc/refman/8.0/en/regexp.html

            • + 2 comments

              thanks sir, It helped me a lot to understand this concept...... Can u pls suggest me from where i should learn sql to strengthen my concept.... I m a beginner so still struggling with new concepts..Thanks in advance.

              • + 0 comments

                Hi. I personally learned the basics of MySQL from CodeAcademy. It took a few hours. Next I did this HackerRank SQL track which was also helpful.

                HackerRank solutions.

              • + 0 comments

                check 'Mike Dame' sql tutors. I personally think he has the best sql tutorials.

            • + 0 comments

              Elegantly put. Glad to have you here helping us understand.

            • + 1 comment

              what the use of ^[aeiou]? it means negation means string not started with 'aeiou', right.

              • + 1 comment

                Correct.

                ^[aeiou]
                

                is city names that *start with vowels"

                ^[^aeiou]
                

                is city names that start with anything that's not a vowel

                HackerRank solutions.

                • + 0 comments

                  ^[^aeiou] it says words that starts other than a,e,i,o,u

            • + 0 comments

              Thanks @RodneyShag!

            • + 0 comments

              even with just [^aeiou] statement I am able to see this strings in my result: Alanson Auburn why is it so?

            • + 0 comments

              Thanks for the explaination.

          • + 2 comments

            try this

            SELECT DISTINCT CITY FROM STATION WHERE REGEXP_LIKE(CITY, '^[^aeiou]','i');

            • + 1 comment

              What the use of seperate 'i'

              • + 1 comment

                FOR case-insensitivity we use 'i'.

                • + 0 comments

                  for ignoring the case sensityvity we have used "i"

            • + 0 comments

              This works for me.

          • + 0 comments

            i too have same doubt plzz... explain

          • + 0 comments

            Putting it inside the brackets makes it match all characters EXCEPT the ones inside the bracket. WHAT ELSE REMAIN? putting it outside makes it match WITH city name STATRS WITH remaining characters i.e..,(consonants)

        • + 2 comments

          Select distinct city from station where city not regexp '^[...aeiou]';

          • + 0 comments

            we should remove these ... inside the brackets and it's working fine! :)

          • + 0 comments

            YES THE CODE IS RUNNING ACCURATELY THANKS FOR PROVIDING THE BEST SOLUTION BRO

        • + 2 comments

          one of the code that i used:

          SELECT DISTINCT CITY FROM STATION WHERE CITY LIKE "[^a|e|i|o|u]%";

          • + 0 comments

            NOT WORKING BRO

          • + 0 comments

            Works with SQL Server

        • + 0 comments

          GOOD EXPLANATION!

        • + 0 comments

          Thanks a lot for share this information

      • + 2 comments

        ^ indicates the beginning of the string. $ indicates the end of the string.

        selects those which dont contain AEIOU in the begining. ^[^AEIOU]

        • + 5 comments
          select distinct city from station where left(city, 1) not in ('a', 'e', 'i', 'o', 'u');
          

          MYSQL

          • + 0 comments

            your solution does not work

          • + 2 comments

            I did exactly same but it shows wrong answer. Don't know why

            • + 0 comments

              select distinct city from station where left(city, 1) not in ('a', 'e', 'i', 'o', 'u') order by city;

            • + 0 comments

              Just replace the vowels present in lowercase with upper case vowels

          • + 0 comments

            SELECT DISTINCT CITY FROM STATION WHERE LEFT(CITY, 1) NOT IN ('A', 'E', 'I', 'O', 'U');

          • + 0 comments

            i try other query like as: select distinct city from station where lower(left(city, 1)) not in ('a', 'e', 'i', 'o', 'u');

          • + 0 comments

            Certainly! The SQL query you provided is used to select distinct cities from the "station" table where the first letter of the city is not a vowel (i.e., not 'a', 'e', 'i', 'o', 'u'). Here's the query:

            sql Copy code SELECT DISTINCT city FROM station WHERE LEFT(city, 1) NOT IN ('a', 'e', 'i', 'o', 'u'); This query will retrieve distinct city names from the "station" table where the first letter of each city does not belong to the specified set of vowels. Adjust the table and column names if they differ in your database schema.

        • [deleted]
          + 1 comment
          [deleted]
      • + 0 comments

        To include all the city name starts with(b,c,--,z etc.,) other than letters inside the bracket(vowels).As simple as that :) , cheers!

      • + 0 comments
        • If you use NOT REGEXP. You just need to use "^" outside the brackets: Not Regexp '^[aeiou]'.
        • If you use only REGEXP. You must add "^" Inside the brackets: Regexp '^[^aeiou]'.
      • + 0 comments

        thats because we still need to tell SQL to look for first character in the string. The first ^ in '^[^aeiou]' points to the first character in the string. the second ^ lets SQL know that we want to exclude these characters.

    • + 1 comment

      As per your answer, the city will not contain vowels at all. They have just asked: not starting with vowels.

      • + 0 comments

        The additional ^ that's the first character in the regular expression makes [^aeiou] apply to just the 1st character of the regular expression. So, the reg. exp. can contain vowels.

        HackerRank solutions.

    • + 2 comments

      My solution was similar, but may be easier to read considering the questions below:

      `mysql SELECT DISTINCT CITY FROM STATION WHERE CITY NOT REGEXP '^[aeiou]'; '

      • + 4 comments

        For oracle .....

        SELECT DISTINCT CITY FROM STATION WHERE LOWER(SUBSTR(CITY,0,1)) NOT IN ('a','e','i','o','u');

        • + 3 comments

          This also works:

          select distinct CITY from STATION where lower(substr(CITY,1,1)) not in ('a','e','i','o','u');

          Can you please tell me the difference in the substring function of the two solutions?

          Sorry I am pretty new to this!

          • + 0 comments

            Because indexing starts from 0. And in negative side it starts from -1.

          • + 0 comments

            quando usa posição '0', ele considera como 1.

          • + 0 comments

            This also works in Oracle: Select distinct city from station where substr(city,1,1) not in ('A','E','I','O','U');

        • + 1 comment

          WHY WE USE LOWER HERE? Can you plz explain.

          • + 0 comments

            The city name starts with the letter capital. To change upper case letters into lower case we use lower here because we are using lower case letters in 'not in' function in where clause. or we can use the below code without using lower by putting upper case letters in 'not in' SELECT DISTINCT CITY FROM STATION WHERE (SUBSTR(CITY,0,1)) NOT IN ('A','E','I','O','U');

        • + 1 comment

          cud u plz explain lower(substr(city,0,1));

          • + 1 comment

            Lower coverts what we get to the Lower Case. substr is used to get some part of the complete string. In this case we will get the (0 to 1) i.e Start from 0 and end at 1 index.

            Example:- Illinois -> Will give result as 'i' if we use lower(substr(city,0,1));

            • + 0 comments

              I dont understand why it would not be upper?? No city would start with lower so why would we want lower in our results???

        • + 0 comments

          It must be LOWER(SUBSTR(CITY,1,1)) there must be 1,1 not 0,1 :)

      • + 4 comments

        For MySQL :-

        SELECT DISTINCT(CITY) FROM STATION WHERE LEFT(CITY,1) NOT IN ('A','E','I','O','U');

        • + 0 comments

          dope answer ! thx

        • + 0 comments

          best

        • + 0 comments

          Thanks..!

        • + 0 comments

          indeed a nice one !! left and right string functions are rarealy used but so helpful

    • + 2 comments

      Hello,

      May I ask if there is any difference if we add .* into the expression:

      SELECT DISTINCT CITY FROM STATION
      WHERE CITY REGEXP '^[^aeiou.*]';
      
      SELECT DISTINCT CITY FROM STATION
      WHERE CITY REGEXP '^[^aeiou].*';
      

      Edit: Based on the comments below, the 1st expression above is not correct because it means the string doesn't start with a,e,i,o,u and also doesn't start with . or *.

      But how about the 2nd expression? I'm wondering whether it is correct or not.

      Thanks!

      • + 1 comment

        Yes, that is the same since .* matches 0 or more of any character.

        HackerRank solutions.

        • + 2 comments

          would the same query will execute for oracle?

          • + 0 comments

            Sorry I've never coded any Oracle.

          • + 0 comments

            SELECT DISTINCT CITY FROM STATION WHERE LOWER(SUBSTR(CITY,1,1)) NOT IN ('a','e','i','o','u');

      • + 1 comment

        In the square brackets, the characters have no special meaning. So if you include .* in the square bracket, it means the string doesn't start with a,e,i,o,u,.,*.

        • + 1 comment

          thanks! then how about the following? is it also valid for this case?

          SELECT DISTINCT CITY FROM STATION
          WHERE CITY REGEXP '^[^aeiou].*';
          
          • + 1 comment

            I think it is valid, but it's redundant to add .*

            • + 0 comments

              I see. Thanks!

    • + 1 comment

      or you could use where city not regexp '^[aeiou]'. This is less confusing

      • + 0 comments

        Agreed, yours is clearer. I'm going to leave my solution as the fancy way since it's good Regex operator practice.

    • + 3 comments

      IT IS NOT WORKING ON ORACLE

      • + 0 comments

        This solution works on MySQL only. Oracle requires different syntax.

        HackerRank solutions.

      • + 0 comments

        For Oracle .....

        SELECT DISTINCT CITY FROM STATION WHERE LOWER(SUBSTR(CITY,0,1)) NOT IN ('a','e','i','o','u');

      • + 0 comments

        Oracle : SELECT distinct CITY FROM STATION WHERE NOT REGEXP_LIKE(city ,'^(A|E|I|O|U)');

    • + 1 comment

      Select distinct CITY from STATION where not CITY like  '[aeiou]%';

      • + 0 comments

        MS SQL SERVER That wrkd... Thanks.

    • + 0 comments

      why like is not working ?

    • + 1 comment

      For Oracle .... SELECT DISTINCT CITY FROM STATION WHERE LOWER(SUBSTR(CITY,0,1)) NOT IN ('a','e','i','o','u');

      • + 1 comment

        What does LOWER(SUBSTR(CITY,0,1)) mean? plz answer im new

        • + 0 comments

          SUBSTR (String, start, length) -> CITY is String & when we start index starts with 1 basically the length it depends u can select upto how much length u wanted to.

          for ex: SUBSTR("LeaRn N Practice",3,5) -> so start at index 3 i.e., 'a' & continue till length of 5 i.e., (aRn N) is the o/p.

          If u want all the charcaters of o/p to be in lower case use LOWER.

    • + 1 comment

      what's the use of ^

      • + 0 comments

        I answered this in an earlier comment

        HackerRank solutions.

    • + 1 comment

      It is showing invalid relational operator

      • + 0 comments

        Hi. Make sure you are using MySQL and the code will work properly.

        HackerRank solutions.

    • + 1 comment

      I DIDN'T UNDERSTAND WHAT YOU ARE EXPLAINING?? WHY WE ARE USING ^ SYMBOL TWICE?

      • + 0 comments

        This comment may answer your question

        HackerRank solutions.

    • + 0 comments

      You can also use the NOT keyword.

      Like so:

      SELECT DISTINCT city FROM station
      WHERE city NOT RLIKE '^[aeiou]'
      
    • + 1 comment

      what is the difference between regexp '^[aeiou]' , regexp '[^aeiou]' and regexp '^[^aeiou]' ?

      • + 1 comment

        Hi. I updated my original comment to answer your question.

        HackerRank solutions.

        • + 0 comments

          Thank you very much

    • + 0 comments

      SELECT DISTINCT CITY FROM STATION WHERE CITY REGEXP '^[^aeiou]';

      Can u please tell me whats wrong with my query

    • + 1 comment

      SELECT DISTINCT CITY FROM STATION WHERE CITY REGEXP '^[^aeiou]';

      Can u please tell me whats the error in my query. Its giving me a runtime error

      Msg 4145, Level 15, State 1, Server WIN-ILO9GLLB9J0, Line 9 An expression of non-boolean type specified in a context where a condition is expected, near 'REGEXP'.

      • + 0 comments

        Hi, are you using MySQL? Your query is the same as the working solution.

        HackerRank solutions.

    • + 1 comment

      what about this SELECT DISTINCT CITY FROM STATION WHERE CITY NOT REGEXP '^[aeiou]';

      does the computational cost same as yours ?

      • + 0 comments

        Yes. Nice job.

        HackerRank solutions.

    • + 0 comments

      SELECT DISTINCT CITY FROM STATION WHERE not( CITY LIKE "A%" or city LIKE
      "E%" or city LIKE "I%" or city LIKE "O%" or city LIKE "%u");

      this is running in mysql

    • + 0 comments

      We can also do that by logical NOT ;)

      SELECT DISTINCT city FROM station WHERE NOT city REGEXP("^[aeiou].*");

    • + 0 comments

      It's working like you tell us, but is working fine with NOT like this way:

      SELECT DISTINCT CITY FROM STATION WHERE CITY NOT REGEXP '^[aeiou]';

    • + 0 comments

      select distinct city from station where city not regexp'^[aeiou]';

    • + 0 comments

      error: ORA-00920: invalid relational operator

    • + 0 comments

      select distinct city from station where substr(city,1,1) NOT IN('a','e','i','o','u');

    • + 0 comments

      That's cool.

      I tried it this way SELECT DISTINCT CITY FROM STATION WHERE NOT REGEXP_LIKE(LOWER(CITY), '^[aeiou]');

    • + 0 comments

      Thanks, this reduces the code.

    • + 1 comment

      How About this :- SELECT DISTINCT CITY FROM STATION WHERE !(REGEXP_LIKE(City, '^[aeiou]'));

      • + 0 comments

        Great job!!! That works fine.

    • + 0 comments

      but why does this thing shows error on hackerrank even if it is right

    • + 0 comments

      what does it mean regexp

    • + 0 comments

      Not Working for MySQL

    • + 0 comments

      why u usre CITY REGEXP WHY NOT CITY LIKE

    • + 0 comments

      > THANK YOU

    • + 0 comments

      SELECT DISTINCT CITY FROM STATION WHERE LEFT(CITY,1) NOT IN ('A','I','E','O','U')

    • + 0 comments

      This is the same solution that I used