Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I want to create a view, named route that selects the road's name and length, which have a greater than or equal to average length. Here is my script

CREATE VIEW longRoads AS
    SELECT 
        name, length
    FROM
        roads
    WHERE
        length >= AVG(length)
    GROUP BY name; 

>Solution :

I suspect your question is about how to use average in this case. You could write as a subquery, but may depend on your version of MySql.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

CREATE VIEW longroads AS
   SELECT 
     name, length
   FROM  
     roads
   WHERE 
     length >= (select avg(length) from roads);

Note: I’m not sure on your intention to have GROUP BY in your query, but I don’t believe you want it. And I would consider changing the name of your "length" column.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading