7 Mistakes Beginners Make with SQL

Поделиться
HTML-код
  • Опубликовано: 19 июн 2024
  • 📝 Get my free SQL Cheat Sheets: www.databasestar.com/get-sql-...
    🎓 Learn and become confident in SQL: databasestar.mykajabi.com/sql...
    Learning SQL can be hard with the number of places to learn from and the functionality of the language.
    As you learn more and get more experienced, you realise some of the things you learned when you were a beginner weren’t the best way to do something.
    But what if you're a beginner?
    What are those things you should avoid?
    In this video, I’ll explain 7 areas or mistakes that I see being made as a beginner, why they should be avoided, and what to do instead.
    Timestamps:
    00:00 What are we learning?
    00:27 Mistake 1 - Using SELECT *
    02:48 Mistake 2 - Writing one query per table
    03:51 Mistake 3 - Not using functions
    05:13 Mistake 4 - Using reserved words for table and column names
    06:30 Mistake 5 - Not using WHERE with DELETE or UPDATE
    08:17 Mistake 6 - Comparing with NULL using =
    09:03 Mistake 7 - Not saving queries to use again
  • НаукаНаука

Комментарии • 30

  • @mostafa2199
    @mostafa2199 Год назад +4

    Your channel helped (and still) me a lot while learning SQL
    Thanks man ♥️
    Keep the good work up

  • @angelbear_og
    @angelbear_og 2 года назад +3

    Another (IMO better) way to do #5 is just write it as a SELECT query to start. You need to validate your records before updating/deleting anyway. Once it's correct you can replace SELECT with DELETE; or write UPDATE/SET clause without the FROM and WHERE; and copy/paste the WHERE from the SELECT query. I often end up with something like this, and once my result set is what I want I flip the commenting from UPDATE to SELECT:
    select COLA, COLB, COLC
    from TBLA
    --update TBLA
    -- set COLA = x
    -- , COLB = y
    -- , COLC = z
    where CRITERIA1
    and CRITERIA2
    etc...

    • @DatabaseStar
      @DatabaseStar  2 года назад +2

      Great advice! Yeah checking the records you are about to change from an Update or Delete by running a Select is very helpful.

  • @MrNummularius
    @MrNummularius 2 года назад

    Thanks!

  • @utkarshgupta6091
    @utkarshgupta6091 2 года назад +1

    Hello sir. Are you planning to cover window functions like over-partition by clause and topics like cte? please consider the employee database that you used in your udemy course as it will be easier to understand from it. please cover these topics.

    • @DatabaseStar
      @DatabaseStar  2 года назад +1

      Good idea! I have it on my list of things to cover in future videos.

  • @TheInternetFan
    @TheInternetFan Год назад +1

    what command will reveal all the columns/attribute of a table without using "SELECT * FROM table" in order to avoid mistake n.1?

    • @DatabaseStar
      @DatabaseStar  Год назад +1

      It depends on your database, but you can usually query the data dictionary to find this information. Something like SELECT * FROM information_schema.columns WHERE table_name = 'person'; - this could work on MySQL to see column information. Other vendors (Oracle, SQL Server, Postgres) have similar features.

  • @luupski
    @luupski Год назад

    Nothing wrong with using select * if you do not know all the columns in the table.
    And to prevent getting to much data, just limit the rows with 'ROWNUM < 10' for example, or like 'fetch first 10 rows only'

    • @DatabaseStar
      @DatabaseStar  Год назад +1

      Good point, yeah using Select * is helpful to see all of the columns. Using it to find out about the rows and columns is a good way to use it. Using it in production - different story!

    • @luupski
      @luupski Год назад

      @@DatabaseStar i do this in production daily and limit the result to 1 row
      I have to use a couple of dozens of tables and can't remember all of the columns in all those tables, only the most common ones used. So i use it whenever i have joins, or want to see if any additional columns help to present better data.
      Sure i can go into an object browser and go and look at the columns there, but that takes much more time.
      As long as it is used wisely i see no issue of using select * in production

  • @Keiktu
    @Keiktu 2 года назад

    Subscribed :)

  • @ayman4318
    @ayman4318 2 года назад

    I need explanation for indexing

    • @DatabaseStar
      @DatabaseStar  2 года назад +1

      I created a video on Indexing about a month ago that could be helpful.

  • @romanpikulenko
    @romanpikulenko Год назад

    The suggestion #2 is absolutely wrong and it may and certainly will cause your application to halt. Let's imagine a real application consisting of something like follows:
    table Orders
    (
    OrderId primary key,
    -- ~ 20 columns with data
    )
    table OrderItems
    (
    OrderId int reference to Order,
    ProductId int reference to Product,
    primary key (OrderId, ProductId)
    -- ~ 8-10 other columns with data
    )
    table Products
    (
    ProductId primary key,
    -- 10 other columns with data
    )
    Now, we need to fetch information about 10 orders with their items and products to construct business logic models. Let's assume that each order has 20 items. In case of one select query which joins all related tables and return one result we will have:
    200 rows - 10 orders * 20 order items
    each row consists of 40 fields - 20 field of order + 10 field of order item + 10 field of product
    So that we will have 200 * 40 = 8000 values fetched from the db
    if we execute three separate queries to receive the same data table by table, we will have:
    10 * 20 = 200 values for orders
    20 * 10 * 10 = 2000 values for order items
    20 * 10 * 10 = 2000 values for products and it is only of all products are unique throughout all orders which is very rear case.
    So that we will have 200 + 2000 + 2000 = 4200 and even less values fetched from the db.
    And this difference grows very quickly as information from new tables are needed because they have to be joined to the one select statement.
    The main problem of a join operation that it duplicates information in rows, this is the reason of difference in field count mentioned above. Moreover, one statement approach does not allow to use caches to reduce common amount of data needed from db.

    • @DatabaseStar
      @DatabaseStar  Год назад +1

      Thanks for the detailed comment. Are you saying it's bad to join separate tables because it could cause your application to halt? If so, I believe you have been mistaken.
      Your example indicates 200 rows would be returned, with data from three tables. This is a tiny amount of data for a database to handle, and even in a high-volume system, there are features and techniques that can be used to improve the performance if it suffers.
      Your scenario of the number of values is OK as well. You may not need to select all values in all tables, but if you do, and you need to select 8000 values (200 rows x 40 values), this is also not a problem for the database to handle.
      If you execute three separate queries, there may be fewer fields returned, as you've shown. But you would still need to combine the results together to show the correct order and product data. If this is not done in the database, you'll need to do it in the application, which is not ideal and would still take some processing.
      There is no problem with returning common data if it relates to different rows. You'll have to combine data in one way - either in the database or the application. The database is designed specifically to do this, so it's better to do it there than the application.

  • @tsunamio7750
    @tsunamio7750 4 месяца назад +1

    7:52 NO NO NO NO! YOU DO NOT RUN AN UPDATE OR DELETE CLAUSE WITHOUT CHECKING WITH SELECT FIRST!
    REDO THIS ADIVE I BEG YOU! Write a SELECT statement and see if the returned data is truly what you wanted to mofiy. Then only then, replace SELECT with UPDATE OR DELETE.

    • @DatabaseStar
      @DatabaseStar  4 месяца назад

      Great point! Yes you should always run the query as a Select first to see the rows that are impacted by the Delete or Update. Then it’s simple to replace it with a Delete or Update.
      My tip about always adding a Where clause to a Delete or Update still stands - perhaps I should have added your suggestion to this video to make it better.

  • @andrewkamoha4666
    @andrewkamoha4666 10 месяцев назад

    *Mistake 3 - Not using functions*
    4:17 "SQL includes many in-built functions to process data and give you a result."
    Where is the *MEDIAN* function ????

    • @DatabaseStar
      @DatabaseStar  10 месяцев назад

      The Median function is not included in many vendors of SQL, but some Google searching indicates it's possible to write a function to calculate it.

  • @mazinalyousef5814
    @mazinalyousef5814 3 месяца назад

    No 5 is disaster