Structured Query Language is a language for interacting with relational databases. Read the tag wiki's guidelines for requesting SQL reviews: 1) Provide context, 2) Include the schema, 3) If asking about performance, include indexes and the output of EXPLAIN SELECT.

learn more… | top users | synonyms

2
votes
1answer
22 views

Updating user log in time while he logs in to the web page

My intention is to store the user's last login date and time when he logs in to the web page. I pass his username and password to the stored procedure to check whether is it correct or not! I believe ...
1
vote
0answers
13 views

Association count in single query for ActiveRecord

This is a small gem that gives the possibility to include counts of reflections when fetching active record objects. What do you think of the code? Any improvements or other (similar) use cases where ...
3
votes
2answers
31 views

Supervisors and employees query, with a subselect and inner join

I want to replace or improve the SELECT command of tblVisor to make it faster. Is there any way to improve this SQL command? ...
4
votes
1answer
28 views

Checking that data has been created in multiple tables

I have 4 tables that I want to check for data. When there are Rows in all the tables, I want to print a message for success. When there aren't any rows in any table I print an error (when 1 or more ...
6
votes
1answer
30 views

Query to find users from where?

Based on my question here, I have updated my query to take user input so it can be a generic query: ...
4
votes
1answer
83 views

Acquiring an ID from a newly inserted entry in an MSSQL database

In an MSSQL database, I have automatically increasing IDs for every entry. Is it safe to acquire the ID of the newly inserted entry like this? I am worried that, if at the exact same time another ...
5
votes
2answers
43 views

1st questions with answers within X hours

I recently challenged @Hosch520 with writing a query as such: Find first questions with answers posted within 24 hours And he did great. I figured I would have a go at it too. Turns out I ...
0
votes
0answers
17 views

Perform SELECT INTO Statement and add Primary Key afterwards

How can I improve this TSQL Statement in terms of performance? ...
3
votes
1answer
39 views

Find first questions with answers posted within 24 hours

So, sᴉɔuɐɹɥԀ suggested I write a SEDE query to find first questions with answers posted within 24 hours, and here it is: ...
6
votes
4answers
1k views

Query to find users from Australia

After a discussion of which users were from Australia, I wrote my first SQL query to find out: ...
4
votes
1answer
65 views

Classes for reading data from Sql Server into objects

The following is a set of classes that read metadata from Sql Server tables and populates local objects. It works, but I feel like there's too much duplication happening and too many places changes ...
3
votes
1answer
107 views

Handling new user requests from a Swift client

I am starting to play with POST request to save in my server the user token so I can send push notifications in Swift (this is the swift code). This is the PHP file that processes the request (please ...
1
vote
1answer
25 views

Bookmaker odds and offers query

The following query returns the latest Odds for each Offer based on the timestamp on the ...
2
votes
1answer
56 views

Controller to list users

I think I put too much code in my controller that was supposed to go in the model. This is a part of my controller, I'm not gonna paste everything since there is a lot of code. ...
8
votes
1answer
49 views

Assigning permissions to new role without RBAR

I'm currently faced with a problem in our SQL Server environments. We need to migrate our existing logins, users and roles to a new login, user and role with the same permissions as the old. I've been ...
0
votes
0answers
18 views

Converting static SQL query to dynamic [migrated]

I've created my first dynamic, OO, SQL query in Drupal 7 and I'd like feedback. I have a static query (below) that works. I'm trying to create a dynamic query so I can use the 'pager' extension. This ...
2
votes
2answers
61 views

Users and friends query with retry, returning JSON

I am new to Azure and SQL. I have the following function that is part of a larger Web-API which is hosted on Azure and is querying an SQL Server database that is also hosted on the Azure platform: ...
6
votes
1answer
43 views

SearchSchoolData - what class where?

I am running a test load using Telerik Test Studio using 100 users at once entering search filters on a page that calls a stored procedure. My application that calls the stored procedure is an asp.net ...
3
votes
2answers
29 views

SQL server efficiency, comparing date time of latest record and the one under it

I have a snippet of SQL that compares the last two records and gives the datediff in seconds. However, the way I have it is quite slow, taking up to 20 seconds to execute depending on how many ...
1
vote
1answer
74 views

Populating an HTML combo box by looped concatenation

This code takes the results of a database query and uses them to populate the options in a combobox on a form. Would it be better to start with an empty htmlCode ...
3
votes
1answer
66 views

Matchmaking system - sorting musicians by skills, genres, and location

I'm making a matchmaking system using PHP and MySQL which allows users to search through a list of musicians so that they can find people with the right skills and interests who are near them. It ...
1
vote
2answers
162 views

Multi-Table Unions [closed]

I've been using joins for years but in this instance I want to be able to do different queries on different tables which may have different structures due to clients. Long term we are aiming to make ...
8
votes
4answers
224 views

Sonar remarks make SQL unreadable?

I ran the following code through Sonar, a static analysis tool. Original file: ...
5
votes
1answer
74 views

PostgreSQL multiple processes and queries or nested query

I have a series of tables and queries I run a process against and at times this process can take hours - if not multiple days - depending on the date range I select. I am running PostgreSQL 9.4. In ...
2
votes
1answer
91 views

SQL Dependency with Broker

I have written a windows service for one of our local servers. This service works like a gem on my local machine, does what it's supposed to (ACCP to exchange db data), but I'm not overly familiar ...
3
votes
2answers
49 views

Eager load additional data about some instructors

Below is a snippet of code of a little practice project. I am still pretty new to EF and I was wondering if there is a way to write the below statement so that all required data is eager loaded since ...
2
votes
2answers
46 views

Query using cross join or inner join

Please consider the following three tables that I am playing with in the image below: I want to come up with a SQL Query which shows first_name, last_name, and the total amount of all orders for ...
1
vote
1answer
134 views

Two tables for storing comments

I have two tables for storing comments comments ...
7
votes
2answers
688 views

Preventing SQL injection by converting all characters to their ASCII values

Scenario In order to prevent SQL injection, I'm converting every character of a string to be inserted in the database into its ASCII value before performing the query; in order to read the value of ...
1
vote
1answer
69 views

Performance shifting for SQL Database and Linq memory grouping

I am messing around with shifting between stored procedures and business layer EF getting data rapidly then processing in memory. This works and on a static data range of 3 months I get execution ...
4
votes
1answer
47 views

Luhn algorithm MS-SQL

This a solution Luhn algorithm for credit validation. This algorithm is a basic one inspired by wikipedia. I mean no check for length, type, and so on. You can enhance the base code as you want it to ...
1
vote
2answers
60 views

SQL Data Fetcher for CMS

I am creating a CMS for a website, so I've added most of the functions that will fetch data from the database to a separate class. The code doesn't seem to have any bugs(Since I've tested it) but I ...
1
vote
0answers
36 views

prestashop: get final product category

In general, in prestashop there are no funcions to really have the final (the one with highest level_depth) category of a product based on id_product, so I extended the Product class and added this ...
5
votes
3answers
106 views

SQL database design for ecommerce

I need to create a database which stores details products, Manufactures,Suppliers. So initially I divided Products into categories and subcategories. These are the tables I created. ...
3
votes
1answer
95 views

Optimizing a Query in Entity Framework

I'm wondering what can be done to optimize the following. I've tried to note the points at which most of the overhead is occurring. The main two points I see are The call to DiffDays from within ...
3
votes
1answer
43 views

Join - order by vote-sum and check if user has voted

I have a vote and a comment table. Now I want to display all comments and order them by votes-sum. Sum because some votes can have a higher value than others. Therefore I want to display if the user ...
0
votes
1answer
58 views

API POST to save a new item in a Database via PHP & MYSQLI

Is this correct MYSQLI implementation? IS it safe from injection? IS the object cleanup done properly? ...
7
votes
1answer
40 views

Stack Exchange Data Explorer query to find upvoted questions with only downvoted answers

I have written a SQL query against the Stack Exchange Data Explorer that identifies answered questions with a positive score for which all the answers have a negative score (this was an answer to this ...
2
votes
2answers
25 views

CROSS JOIN solution for records “missing” from a link table

Use case: We have a simple two tables plus a link table setup using some modern RDBMS. For example: Hospital table (id, name, ...
1
vote
1answer
33 views

Select Queries From Parent-Child Tables Model

I have two tables (parent-child model): Loans & Transactions. Each Loan row has multiple Transaction rows. I want to select all rows from Loans together with most recent row from Transactions. ...
6
votes
1answer
37 views

Implementing EXCEPT functionality for social networking exercise

I am taking Stanford's Introduction to Databases Self-Paced online course. I have gone through the videos in the SQL mini-course, and I am having trouble completing the exercises. The following is ...
13
votes
0answers
178 views

Complicated article sort

In short I have a bunch of articles (~100k) which shall be ranked ("mixed", hence "newsmix") based on their newsValue (how important they are, between 1 and 10) and newsLifetime (how long they stay ...
2
votes
1answer
44 views

Simplify SQL query that utilizes GROUP BY and HAVING BY and uses aggregation

I am taking Stanford's Introduction to Databases Self-Paced online course. I have gone through the videos in the SQL mini-course, and I am having trouble completing the exercises. The following is ...
3
votes
1answer
40 views

Query about advertiser calls times out

I'm a bit of a rookie MySQL coder and I've been tasked with building a query that will join and filter several very large tables (one with ~210 million rows, one with 10 million, one with 2.5 million, ...
4
votes
3answers
160 views

Stack Exchange Data Explorer T-SQL to Find Users by Most Badges

I very infrequently use SQL. I need more experience, so I have created a data explorer query to find those users with most of any particular badge (have to edit the query itself right now to alter ...
3
votes
1answer
29 views

Return most recent non null field values for multiple unknown fields

I am attempting to create a stored procedure that looks at one table and imports changes in that table to another. This stored procedure is going to be preformed on multiple source tables and will ...
6
votes
1answer
80 views

Generate random records for table in SQL

I'm doing a project in ms sql and I have to fill table with random records. Here is a definition of table: ...
3
votes
2answers
127 views

Finding max rating for a movie

I am taking Stanford's Introduction to Databases Self-Paced online course. I have gone through the videos in the SQL mini-course, and I am having trouble completing the exercises. The following is ...
5
votes
1answer
43 views

Finding bad Narutos

While looking for Naruto posts (= overlooked accepted answers well worthy of upvotes), I noticed some odd false positives, selfie answers, where the user who asked the question accepted his own answer ...
7
votes
1answer
96 views

Finding answers where happy customers commented “thanks”

To find answers where users expressed their gratitude (by mentioning "thank" in comments), I put together this SEDE query (latest): ...