When to use a CROSS JOIN, and grouping rows of text together using the STRING_AGG function.

Sdílet
Vložit
  • čas přidán 11. 09. 2024
  • You should avoid the CROSS JOIN unless you have a good reason. But what are those reasons?
    My SQL Server Udemy courses are:
    70-461, 70-761 Querying Microsoft SQL Server with T-SQL: rebrand.ly/que...
    98-364: Database Fundamentals (Microsoft SQL Server): rebrand.ly/dat...
    70-462 SQL Server Database Administration (DBA): rebrand.ly/sql...
    Microsoft SQL Server Reporting Services (SSRS): rebrand.ly/sql...
    SQL Server Integration Services (SSIS): rebrand.ly/sql...
    SQL Server Analysis Services (SSAS): rebrand.ly/sql...
    Microsoft Power Pivot (Excel) and SSAS (Tabular DAX model): rebrand.ly/mic...
    ----
    Unlike the other joins, CROSS JOINs does not use the ON clause to compare rows. Instead, CROSS JOINS takes each row in one table, and joins it to every row in the second table. This means that the number of rows in the resulting query can be massive.
    In this video, we will look at animals in a zoo, and ask the question: what animals has each animal seen, based on when they were at the zoo? They would not have seen animals which arrived after they left, or left before they arrived.
    We'll look at answering this question using a CROSS JOIN. We'll also tidy it up using the STRING_AGG function, which was introduced in SQL Server 2017.
    Here is the code to generate the ZooAnimals table:
    DROP TABLE IF EXISTS ZooAnimals;
    CREATE TABLE ZooAnimals
    (ID int primary key,
    Animal varchar(10),
    StartDate date,
    EndDate date);
    INSERT INTO ZooAnimals
    VALUES (1, 'Lion', '2023-01-01', '2023-06-30'),
    (2, 'Tiger', '2023-03-01', '2023-05-30'),
    (3, 'Elephant', '2023-06-01', '2024-01-31'),
    (4, 'Monkey', '2023-05-01', '2023-05-31'),
    (5, 'Giraffe', '2023-08-01', '2023-12-31');

Komentáře • 14

  • @supersql8406
    @supersql8406 Před rokem +3

    Jeeze, finally someone did a Cross Join tutorial with an elegant explanation!

  • @WillijahDawson
    @WillijahDawson Před 10 měsíci +3

    Great explanation i appreciate the excel comparison

  • @vickarkhan
    @vickarkhan Před 2 lety +4

    Such a beautiful explanation. Thanks for the video.

  • @user-th9rk5iy1d
    @user-th9rk5iy1d Před 8 měsíci +1

    As I heard the voice instantly I knew it is Mr. Burton, Thanks for your udemy course.

  • @abdulkadirdemirci3242
    @abdulkadirdemirci3242 Před rokem +3

    wonderfull explanation on that topic with perfect example as wondefull as your expression, best regards

  • @basheeral-momani2032
    @basheeral-momani2032 Před rokem +1

    thank you for the butifull example and explanation

  • @minnrick7986
    @minnrick7986 Před 2 lety +1

    Superb explanation, thank you.

  • @vijayanandp8804
    @vijayanandp8804 Před rokem +1

    Awesome example that thank you

  • @BTS-en1db
    @BTS-en1db Před 2 lety +1

    Thanks for the vedio

  • @sinjansengupta178
    @sinjansengupta178 Před 4 měsíci

    can't it be done using self join concept?

  • @Retrosenescent
    @Retrosenescent Před rokem +1

    Why did you use a cross join and then filter rather than just using an inner join?

    • @SQLServer101
      @SQLServer101  Před rokem

      Hi Emberfly. That is another option. You would put all of the WHERE into the ON. If you wanted all of the rows in Table1 matched against all of Table2 (as per 3' of this video), then you can't do that with an INNER JOIN easily. Phillip

  • @petrdanilov3122
    @petrdanilov3122 Před 2 lety +1

    Great